jakartaee / jakartaee/rest

Response/ResponseBuilder should be Generic

Open
#20 8 comments 0 reactions 1 assignee Claimed by @glassfishrobot View on GitHub
Dominant language
Java
Stars
400
Forks
143
PR merge metrics
No merged PRs in 30d

Description

As discussed in this thread, ideally Response/ResponseBuilder would be generic
to better support static discovery of types for WADL.

A couple of methods were discussed. Paul Sandoz's original proposal made use of
the . notation:

```
public class Main {

public static class Response {

private final T entity;

private Response(T entity) {
this.entity = entity;
}

public T getEntity() {
return entity;
}

public static class ResponseBuilder {
private T entity;

public ResponseBuilder entity(T entity) {
this.entity = entity;
return this;
}

public Response build() {
return new Response(entity);
}

static protected ResponseBuilder newInstance() {
return RuntimeDelegate.getInstance().createResponseBuilder();
}
}

static public ResponseBuilder start() {
return new ResponseBuilder();
}

}

public static class RuntimeDelegate {
public static RuntimeDelegate getInstance() {
return new RuntimeDelegate();
}

public Response.ResponseBuilder createResponseBuilder() {
return new Response.ResponseBuilder();
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Response r = Response.start().entity("xx").build();

String e = r.getEntity();
System.out.println(e);
}
}
```

And then I put forward a modified version that removed the need for using the
. notation by creating a new interim typed version of the Builder:

```
// Generified Response public class Response {

static class GListString extends GenericEntity> {
public GListString(List list) {
super(list);
}
}

private final T entity;

private Response(T entity) {
this.entity = entity;
}

public T getEntity() {
return entity;
}

public static ResponseBuilder start() {
return new ResponseBuilder("Some State");
}

public static class ResponseBuilder {

private Object state;
private Type type;
private T entity;

public ResponseBuilder(Object state) {
this.state = state;
}

public ResponseBuilder(Object state, Type type, T object) {
this(state);
this.type = type;
this.entity = object;
}

public ResponseBuilder header(String name, String value) {
return this;
}

public > ResponseBuilder entity(K ent) {
return new ResponseBuilder(state, ent.getType(), ent.getEntity());
}

public ResponseBuilder entity(T ent) {
return new ResponseBuilder(state, ent.getClass(), ent);
}

public Response build() {
return new Response(entity);
}

}

public static void main(String[] args) {

List list = new ArrayList();
GListString genericList =
new GListString(list);

Response> response = Response
.start()
.header("Content-Type", "fudge")
.entity(list)
.build();

// Using generic entity
//
Response> genericResponse = Response
.start()
.header("Content-Type", "fudge")
.entity(genericList)
.build();

// String example
//

Response stringResponse = Response
.start() // ResponseBuilder
.entity("String") // ResponseBuilder
.header("Content-Type", "cheese") // ResponseBuilder
.build();
}
}
```

It is possible that neither solution is correct; but it would be good to look
for a modification that covers some of the ideas in the 2.x spec release.
#### Environment
Operating System: All
Platform: All
URL: [http://n2.nabble.com/xml-schema-in-request-and-response-wadl-td3943317.html#a3943317](http://n2.nabble.com/xml-schema-in-request-and-response-wadl-td3943317.html#a3943317)
#### Affected Versions
[2.1]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.