users@jersey.java.net

issue with generics in interface method parameter, POST and PUT methods

From: Jarmo Torvinen <jarmo.torvinen_at_iki.fi>
Date: Thu, 17 Dec 2009 12:53:12 +0200

Hi,

I have:

@MappedSuperclass
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractObject implements Serializable {
...
}

@XmlRootElement(name = "Car")
@XmlAccessorType(XmlAccessType.FIELD)
public class Car extends AbstractObject {
...
}

public interface CrudRestService<T extends AbstractObject> {

        @POST
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_JSON)
        public T create(T newObject);

        @GET
        @Path("{id}")
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_JSON)
        public T read(@PathParam("id") long id);

       ...(same for update and delete)

}

public interface CarService extends CrudRestService<Car> {

}


@Path("/cars")
public class CarWebService implements CarService {

        public Car create(Car newObject) {

               return new Car(newObject);

        }

        public Car read(long id) {

                return findCarById(id);
        }


}

Now, what is happening in real life: the read() function with
GET-method works, but the create() method with POST is not working,
instead, a 405 not allowed is returned.

I traced the issue to AnnotatedMethod.findAnnotatedMethod() call:

try {
            m = c.getMethod(m.getName(), m.getParameterTypes());
        } catch (NoSuchMethodException ex) {
            return null;
        }

When this is called on a interface that uses generics, the function
parameters are not matched and so the annotated method is not
discovered. Are there any options to get this fixed or do I just need
to move the annotations to the implementation class?



-- 
Jarmo