3

I'm creating some resource class with same form so a good idea is use DRY and use inheritance. So I've create a RootResource class and put some methods there. I want to annotate them and then implement them in subclass but it doesn't work! Here is a sample code:

public abstract class RootResource { @GET @Path("/{id: .*}") public abstract String getInfo(String uid); } @Path("/user") public class UserResource extends RootResource{ public String getInfo(@PathParam("id") String uid) { System.out.println("Hello!"); } } 

I'm using jersey 2.6. Any Idea? Thanks.

2
  • This example doesn't even compile.. the return type of String isn't FullDTO... Can you try putting the path param in the method signature on the abstract class? Did you try interface? Commented Jul 12, 2014 at 23:00
  • Yes, I've correct it in my question. I need RootResource be an abstract class. Also adding PathParam to RootResource.getInfo signature doesn't help. Commented Jul 13, 2014 at 0:50

2 Answers 2

7

I've been through the same issue while using Jersey. The Java EE standard for JAX-RS states the following:

3.6 Annotation Inheritance

JAX-RS annotations MAY be used on the methods and method parameters of a > super-class or an implemented interface. Such annotations are inherited by a corresponding sub-class or implementation class method provided that method and its parameters do not have any JAX-RS annotations of its own. Annotations on a super-class take precedence over those on an implemented interface. The precedence over conflicting annotations defined in multiple implemented interfaces is implementation specific.

If a subclass or implementation method has any JAX-RS annotations then all of the annotations on the super class or interface method are ignored.

While Jersey as the reference implementation is very strict with this statement, Resteasy implementation is more lenient and did the trick for me.

Sign up to request clarification or add additional context in comments.

Comments

0

It's important to specify the path over the class since it's the root resource class so the it will get where to look at the class loading and not for individual overridden methods:

 @Path("/account/member/") public class RootResource { . . . 

2 Comments

The root class is an abstract class so I get a java.lang.InstantiationException exception. Thanks for your fast answer!
ah you need to put it on your derived class

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.