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: Mon, 6 Sep 2010 11:12:26 +0200

On Sep 6, 2010, at 9:01 AM, ManiKanta G wrote:

>
> Hi,
>
> Excuse me for the late reply. Please follow my inline comments.
>
>
> This is a deployment error.
>
> I didn't get this. You mean the way I deploy the app or any Jersey
> build error?

No. Jersey is detecting an error with the ChildResource class and will
not deploy the application because of that 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.
>
> Yeah, I can guess this behavior. But I m clueless as when accessing
> the parent resource (/parent) also I m getting this conflict.

There is only one conflict, for the ChildResource class. It makes no
difference if the super class is annotated @Path or not, it is not
taken into account and there is no hierarchical URI relationship
between the two. i.e. there will be two paths:

   /parent
   /child

and not two paths:

   /parent
   /parent/child


> I suppose parent resource will not look into child resource. Isn't it?
> just quoting part of my original mail:
> 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.
>
>
> 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";
> }
> }
>
>
> Yeah, I tried this. But the child method is returning incompatible
> object type with the parent method return type (as Java only allows
> same or co-variant return types), I m required to change the name
> (but that difference was not shown in this example; I just made the
> example simple to demonstrate the behavior)
>

If you want you can change the return type to Object. But it may be
better to do something like this:

@Path("parent")
public class Parent extends AbstractResource { ... }

@Path("parent/child")
public class Child extends AbstractResource { ... }

If you need to share some common functionality.

Paul.