gate sumson
gate sumson

Reputation: 163

Annotation Inheritance in jersey

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.

Upvotes: 3

Views: 3348

Answers (2)

Alberto Segura
Alberto Segura

Reputation: 755

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.

Upvotes: 7

GingerHead
GingerHead

Reputation: 8230

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 {
             . . .

Upvotes: 0

Related Questions