dev@jersey.java.net

Re: Issues when using inheritance in Jersey

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 10 Dec 2007 17:50:58 +0100

Paul Sandoz wrote:
>> 2- I have a lot of resources which perfomrs CRUD operations on
>> entities. All of these classes has a super class (lets say
>> BaseEntityResource). The source could is similar to this: abstract
>> class BaseEntityResource<EntityType> {
>>
>> @UriTemplate("{id}/") @HttpMethod("PUT")
>> @ConsumeMime("application/xml") public void Update(@UriParam("id")
>> Integer id, EntityType entity) { /// ..... }
>>
>> }
>>
>> The problem is that when Jersey looks for entity provider to
>> deserialize the request for EntityType parameter, it ignores the fact
>> that it's a generic type and passes it as it is. So the passed type
>> id Object and as such then JAXB entity provider is not returned. A
>> solution without modifying Jersey, would be by overriding the method
>> in each sub class, redeclaring all of the annotations, and calling
>> the method of the super class. As it seems, duplicated code and
>> inflexibility. It would be very nice if this problem had a solution.
>> Since in cases where the application has 10s of resource (as in
>> mine), this has a huge effect.
>>
>>
>
> Interesting case, i have added a (failing) unit test so we can track this.
>
> I think this is possible to support, although working with reflection
> *and* generics is never easy.
>
> We need to determine the Java type from the type variable associated
> with the method parameter. To do this the code that analyzes the method
> signatures needs access to the concrete resource class. Then that type
> variable needs to be matched against the parameterized types of the
> super classes.
>

There is a fix in the trunk, see this unit test [1].

Currently this is only supported for type variables that correspond to a
request entity, but it could easily be used for type variables of
annotated parameters as well (although i am not sure how useful that
would be given that such annotations are often contextual to the request).

Also the implementation could be a little fragile... this code is hard
to get right, especially when there are multiple layers of inheritance
with type variables passed as type parameters to super classes, so we
could definitely do with some more unit tests.

Paul.

[1]
https://jersey.dev.java.net/source/browse/*checkout*/jersey/trunk/jersey/test/com/sun/ws/rest/impl/resource/GenericInheritanceTest.java

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109