Annotation Inheritance in jersey

烂漫一生 提交于 2019-12-06 12:14:39

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/24717899/annotation-inheritance-in-jersey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!