You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Thibaut Robert <th...@gmail.com> on 2013/08/07 12:06:30 UTC

Generic JAX-RS service class and generic type resolution

Hi!

I would like to share a strange behaviour I have with cxf JAX-RS frontend.
I am writing a generic JAX-RS service class like this:

public class AbstractRestController<T extends RestResource> {
    @POST
    public Response add(final T resource) { ... }
}

where RestResource is an interface.

Then I use a concrete implementation:
public class ConcreteRestController exends
AbstractRestController<ConcreteRestResource> { ... }

I have an error when a post a request, caused by the way the generic type T
is resolved:
org.codehaus.jackson.map.JsonMappingException: Can not construct instance
of cxfjaxrs.RestResource, problem: abstract types can only be instantiated
with additional type information

T is resolved as RestResource instead of ConcreteRestResource.

I found out that in the method InjectionUtils.getSuperType(), it prefers
using the type parameter bound when present, instead of calculating the
real type argument. Is there any motivation beyond this ? In this case, the
actual argument type can be determined at runtime, and is better that the
parameter bound.

I got things working by using this trick: public class
AbstractRestController<T extends Object & RestResource> (because in this
case it only considers the first bound, and thus go beyond to find a type
more precise than Object)

Is there any way to fix this ?

I tested with 2.7.7-SNAPSHOT and 3.0.0-SNAPSHOT. Things works out of the
box with Jersey.

BR,
Thibaut