与传承JAX-RS [英] Inheritance with JAX-RS
问题描述
我使用JAX-RS为我的web服务。我有共同的功能,并想用继承。我提供简单的CRUD操作。我ahve定义的接口,像这样:
公共接口ICRUD { @POST
@Consumes(应用/ JSON)
@Produces(应用/ JSON)
@Path(创造)
公共字符串createREST(字符串transferObject); @得到
@Consumes(应用/ JSON)
@Produces(应用/ JSON)
@Path(retreive /(编号))
公共字符串retreiveREST(@PathParam(ID)字符串ID); @POST
@Consumes(应用/ JSON)
@Produces(应用/ JSON)
@Path(更新)
公共无效updateREST(@Suspended最终AsyncResponse asyncResponse,
最后弦乐transferObject); @删除
@Consumes(应用/ JSON)
@Produces(应用/ JSON)
@Path(删除/(编号))
公共字符串deleteREST(@PathParam(ID)字符串ID);
}
我有一个实现此接口的抽象类:
公共抽象类BaseREST实现ICRUD {私人最终ExecutorService的ExecutorService的= Executors.newCachedThreadPool();@覆盖
公共字符串createREST(字符串transferObject){
返回创建(transferObject).toJson();
}@覆盖
公共字符串retreiveREST(@PathParam(ID)字符串ID){
返回retreive(ID).toJson();
}
@覆盖
公共字符串deleteREST(
@PathParam(ID)字符串ID){
返回删除(ID).toJson();
}@覆盖
公共无效updateREST(@Suspended最终AsyncResponse asyncResponse,最后弦乐transferObject){
executorService.submit(新的Runnable(){
@覆盖
公共无效的run(){
asyncResponse.resume(doUpdateREST(transferObject));
}
});
}}
最后,我实现类SI mply提供了资源的路径:
@Path(会议)
公共类MeetingRestServices
扩展BaseREST
{
}
当我试图在(假设上下文根为/)来访问我的资源:
的http://本地主机:8080 / webresources /会议/ retreive / 0
我得到一个404,它说,它无法找到它。我的想法是,在继承的地方,它与在那里我认为资源应该是路径搞乱。任何对此的思考?
修改
webresources定义如下。此类由Netbeans的自动添加。
@ javax.ws.rs.ApplicationPath(webresources)
公共类ApplicationConfig扩展应用{ @覆盖
公开组<班级<>> getClasses(){
SET<班级<>>资源=新java.util.HashSet中的<>();
addRestResourceClasses(资源);
返回资源;
} / **
*不要修改addRestResourceClasses()方法。
*它会自动填充
*在项目中定义的所有资源。
*如果需要,注释掉调用getClasses)这个方法(。
* /
私人无效addRestResourceClasses(SET<班级<>>资源){
resources.add(com.dv.meetmefor.ws.impl.BinaryDataRestService.class);
resources.add(com.dv.meetmefor.ws.impl.ImageRestServices.class);
resources.add(com.dv.meetmefor.ws.impl.LocaleRestService.class);
resources.add(com.dv.meetmefor.ws.impl.MeetU prestServices.class);
resources.add(com.dv.meetmefor.ws.impl.MeetingRestServices.class);
resources.add(com.dv.meetmefor.ws.impl.UserAccountRestServices.class);
}}
在上面所描述什么好看。下面是其中根据您提供你所秉承的JAX-RS继承的规则。
从JAX-RS规范:
JAX - RS注释可以对实施了超类或方法和方法参数可用于
接口。这种注释是由一个相应的子类或实现类继承
方法提供的方法和它的参数不具有其自己的任何JAX-RS注解。
注释
在超一流采取precedence对这些已实现的接口上。如果子类或实现
方法有任何JAX-RS注解,那么所有的超类或接口方法的注释
将被忽略。例如:
块引用>公共接口ReadOnlyAtomFeed {
@GET @Produces(应用程序/原子+ XML)
订阅getFeed();
}@Path(饲料)
公共类ActivityLog实现ReadOnlyAtomFeed {
公共提要getFeed(){...}
}在上面,
ActivityLog.getFeed
继承了@GET
和@Produces
从界面注释。
相反的:@Path(饲料)
公共类ActivityLog实现ReadOnlyAtomFeed {
@Produces(应用程序/原子+ XML)
公共提要getFeed(){...}
}在上面,在
@GET
注释ReadOnlyAtomFeed.getFeed
不受ActivityLog
.getFeedI am using JAX-RS for my webservices. I have common functionality and would like to use inheritance. I am providing simple CRUD operations. I ahve defined an interface like so:
public interface ICRUD { @POST @Consumes("application/json") @Produces("application/json") @Path("create") public String createREST(String transferObject); @GET @Consumes("application/json") @Produces("application/json") @Path("retreive/{id}") public String retreiveREST(@PathParam("id") String id); @POST @Consumes("application/json") @Produces("application/json") @Path("update") public void updateREST(@Suspended final AsyncResponse asyncResponse, final String transferObject) ; @DELETE @Consumes("application/json") @Produces("application/json") @Path("delete/{id}") public String deleteREST(@PathParam("id") String id); }
I have an abstract class that implements this interface:
public abstract class BaseREST implements ICRUD{ private final ExecutorService executorService = Executors.newCachedThreadPool(); @Override public String createREST(String transferObject) { return create(transferObject).toJson(); } @Override public String retreiveREST(@PathParam("id") String id) { return retreive(id).toJson(); } @Override public String deleteREST( @PathParam("id") String id) { return delete(id).toJson(); } @Override public void updateREST(@Suspended final AsyncResponse asyncResponse, final String transferObject) { executorService.submit(new Runnable() { @Override public void run() { asyncResponse.resume(doUpdateREST(transferObject)); } }); } }
And lastly, my implementing class si mply provides a PATH for the resource:
@Path("meeting") public class MeetingRestServices extends BaseREST { }
When I try to access my resource at (assuming the context root is /):
http://localhost:8080/webresources/meeting/retreive/0
I get a 404, it says it can not find it. My thoughts are that somewhere in the inheritance, it is messing with the path of where I think the resource should be. Any thoughts on this?
EDIT
webresources is defined below. This class is added automatically by Netbeans.
@javax.ws.rs.ApplicationPath("webresources") public class ApplicationConfig extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new java.util.HashSet<>(); addRestResourceClasses(resources); return resources; } /** * Do not modify addRestResourceClasses() method. * It is automatically populated with * all resources defined in the project. * If required, comment out calling this method in getClasses(). */ private void addRestResourceClasses(Set<Class<?>> resources) { resources.add(com.dv.meetmefor.ws.impl.BinaryDataRestService.class); resources.add(com.dv.meetmefor.ws.impl.ImageRestServices.class); resources.add(com.dv.meetmefor.ws.impl.LocaleRestService.class); resources.add(com.dv.meetmefor.ws.impl.MeetUpRestServices.class); resources.add(com.dv.meetmefor.ws.impl.MeetingRestServices.class); resources.add(com.dv.meetmefor.ws.impl.UserAccountRestServices.class); } }
解决方案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:
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 onReadOnlyAtomFeed.getFeed
is not inherited byActivityLog .getFeed
这篇关于与传承JAX-RS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!