Inheritance with JAX-RS

后端 未结 1 387
滥情空心
滥情空心 2020-12-08 21:14

I am using JAX-RS for my web services. I have common functionality and would like to use inheritance. I am providing simple CRUD operations. I have defined an i

相关标签:
1条回答
  • 2020-12-08 21:41

    What you've described above looks good. Here are the rules for JAX-RS inheritance which based on what you've provided you are adhering.

    From JAX-RS spec §3.6:

    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. 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. E.g.:

    public interface ReadOnlyAtomFeed {
        @GET @Produces("application/atom+xml")
        Feed getFeed();
    }
    
    @Path("feed")
    public class ActivityLog implements ReadOnlyAtomFeed {
        public Feed getFeed() {...}
    }
    

    In the above, ActivityLog.getFeed inherits the @GET and @Produces annotations from the interface. Conversely:

    @Path("feed")
    public class ActivityLog implements ReadOnlyAtomFeed {
        @Produces("application/atom+xml")
        public Feed getFeed() {...}
    }
    

    In the above, the @GET annotation on ReadOnlyAtomFeed.getFeed is not inherited by ActivityLog .getFeed

    0 讨论(0)
提交回复
热议问题