users@jersey.java.net

Jersey resources with inheritance giving 'Producing media type conflict' exception

From: ManiKanta G <go4mani_at_gmail.com>
Date: Thu, 2 Sep 2010 14:30:59 +0530

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.

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