users@jersey.java.net

Re: [Jersey] Jersey resources with inheritance giving 'Producing media type conflict' exception

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Thu, 2 Sep 2010 11:45:50 +0200

Hi,

This is a deployment error. Resources classes will inherit methods
from a super class (as is the case with normal inheritance), and in
this case there is a conlifct for ChildResource. There are two
resource methods producing the same media type resulting in an
ambiguity on what method to invoke.

You need to do this:

@Path("/child")
public class ChildResource extends ParentResource {
     @Override
     @GET
     @Produces("text/plain")
     public String getFromParent() {
         System.out.println("Child resource method");
         return "Response from child";
     }
}


On Sep 2, 2010, at 11:00 AM, ManiKanta G wrote:

>
> Hi all,
>
> While I was writing some resource classes, I came across some
> peculiar behavior (at least to me!) of Jersey ( or may be JAX-RS).
>
> I've resource class which extends another resource class, like:
>
> @Path("/child")
> public class ChildResource extends ParentResource {
> @GET
> @Produces("text/plain")
> public String getFromChild() {
> System.out.println("Child resource method");
> return "Response from child";
> }
> }
>
> @Path("/parent")
> public class ParentResource {
> @GET
> @Produces("text/plain")
> public String getFromParent() {
> System.out.println("Parent resource method");
> return "Response from parent";
> }
> }
>
> No matter how I invoke either resource (/child or /parent), I m
> getting the below error:
>
> SEVERE: The following errors and warnings have been detected with
> resource and/or provider classes:
> SEVERE: Producing media type conflict. The resource methods public
> java.lang.String com.jerseydemo.ParentResource.getFromParent() and
> public java.lang.String com.jerseydemo.ChildResource.getFromChild()
> can produce the same media type
> Sep 2, 2010 2:21:24 PM com.sun.grizzly.http.servlet.ServletAdapter
> doService
> SEVERE: service exception:
> com.sun.jersey.spi.inject.Errors$ErrorMessagesException
> at
> com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:150)
> at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:117)
> at
> com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:183)
>
>
> As far I see in the JAX-RS or Jersey documentation, if a resource is
> having the appropriate method with the equivalent request method
> designator (here @GET) and the matching MIME type, it'll serve the
> request, and if it doesn't have the correct match its super class
> will be checked for the match.
>

There is no traversal up the class hierarchy. All available public
methods of a class are treated at the same level. Basically the JAX-RS
implementation will use:

http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getMethods%28%29

public Method[] getMethods() throws SecurityException
Returns an array containing Method objects reflecting all the public
member methods of the class or interface represented by this Class
object, including those declared by the class or interface and those
inherited from superclasses and superinterfaces.

Hth,
Paul.

> But I didn't see/know even if the resource is having the matching
> method, parent class will be checked and throws an error like above.
>
> Most confusing behavior (yeah, even than the above) is parent
> resource is requested, through /parent, still I m getting this media
> type ambiguity error. When a resource was request why is it
> searching in its child resource?
>
> If I m missing some reading or useful concept, please help.
>
> ManiKanta G
> twitter.com/ManiKantaG