On Nov 6, 2009, at 6:23 PM, gerard davison wrote:
>>>> 3. Response type
>>>>
>>>> This would be a strong argument for adding a generic parameter to
>>>> the Response/ResponseBuilder interfaces;
>>>
>>> Marc and I tried and failed miserably. We could not get things to
>>> work with type safety using the response building pattern.
>>>
>> Yes, I forget the exact problem no but we couldn't get it to work
>> satisfactorily. We settled on the GenericEntity class to capture
>> the generic type for use with Response or non-committal return
>> types like Object. Unfortunately this only works at runtime and
>> isn't any use for WADL generation.
>
> I would be interesting to understand better why it didn't work for
> you if you have time.
Oh crap... often when one experiments with a fresh mind one can find a
solution (see end of email).
The problem we were having was we could not connect the T of the
static method on Response to the T of the Response.ResponseBuilder.
There is a less known (well at least to me at the time) of declaring T
right before the declaration of the static method call, for example:
List<String> l = Collections.<String>emptyList();
<blush/> this is all rather embarrassing...
Unfortunately it is too late to make changes to JAX-RS 1.1. I could
add something to Jersey in the interim.
Paul.
public class Main {
public static class Response<T> {
private final T entity;
private Response(T entity) {
this.entity = entity;
}
public T getEntity() {
return entity;
}
public static class ResponseBuilder<T> {
private T entity;
public ResponseBuilder<T> entity(T entity) {
this.entity = entity;
return this;
}
public Response<T> build() {
return new Response<T>(entity);
}
static protected <T> ResponseBuilder<T> newInstance() {
return
RuntimeDelegate.getInstance().<T>createResponseBuilder();
}
}
static public <T> ResponseBuilder<T> start() {
return new ResponseBuilder<T>();
}
}
public static class RuntimeDelegate {
public static RuntimeDelegate getInstance() {
return new RuntimeDelegate();
}
public <T> Response.ResponseBuilder<T>
createResponseBuilder() {
return new Response.ResponseBuilder<T>();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Response<String> r =
Response.<String>start().entity("xx").build();
String e = r.getEntity();
System.out.println(e);
}
}