users@jersey.java.net

Re: [Jersey] Implementing a generic javax.ws.rs.core.Response

From: Jaka Jančar <jaka_at_kubje.org>
Date: Mon, 30 Mar 2009 18:12:49 +0200

On 30. Mar 2009, at 17:45, Paul Sandoz wrote:
> On Mar 30, 2009, at 4:46 PM, Jaka Jančar wrote:
>> But if I understand JAX-RS (table 3.1 in particular) correctly,
>> this would break generic type recognition (it would fall in the 3rd
>> row category), which I need in my entity providers.
>>
>> So the only solution, it appears, would be to define MyResponse in
>> such a way that it would return GenericEntity:
>>
>> public class MyResponse<T> extends javax.ws.rs.core.Response {
>> private T entity;
>>
>> public void setEntity(T entity) {...}
>> public GenericEntity<T> getEntity()
>> {
>> return new GenericEntity<T>(entity) {};
>> }
>> ...
>> }
>>
>> Does anyone see any problems with this?
>>
>
> Yes, you are not setting the genericType when creating the
> GenericEntity thus things will not work for:
>
> MyResponse<List<Bean>>
>
>

Is this problem solvable without modification of Jersey? You said that
you gave up on generic Response because of the builder pattern. I
don't mind not using ResponseBuilder. Doing "return new
MyResponse(code, headers, entity);" is acceptable to me.

All I can think of is doing reflection of type within my resource
methods and passing that to MyResource. Or, alternatively, passing the
Method to MyResponse and then doing the reflection there. But this is
all very ugly.

>> A concern I have is that, because I would like to stay Jersey-
>> independent, I would have to implement MultivaluedMap (for
>> Response#getMetadata()), but the implementations in Jersey seem to
>> do much more than the trivial implementation of the interface, so
>> I'm worried that something will break.
>>
>
> Note that the returned map may be modified by the runtime. Jersey's
> implementation ensures that keys are case insensitive. If you do not
> support case-insensitive keys things could be problematic.
>
> I recommend you do the following. Create a dummy Response and use
> the MultivaluedMap instance from that e.g.
>
> Response.ok().build().getMetadata()
>
Good idea. Thanks!