java 继承 JAX-RS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25916796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inheritance with JAX-RS
提问by user489041
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 interface like so:
我正在使用JAX-RS
我的网络服务。我有通用的功能,想使用继承。我正在提供简单的 CRUD 操作。我已经定义了一个这样的接口:
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("retrieve/{id}")
public String retrieveREST(@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 simply 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?
我得到一个 404,它说它找不到它。我的想法是,在继承中的某个地方,它扰乱了我认为资源应该在哪里的路径。对此有何想法?
EDIT
编辑
webresources is defined below. This class is added automatically by Netbeans.
webresources 定义如下。这个类是由 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);
}
}
回答by cmd
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.
你上面描述的看起来不错。以下是 JAX-RS 继承的规则,这些规则基于您所提供的内容。
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.:
JAX-RS 注释可以用于超类或已实现接口的方法和方法参数。此类注释由相应的子类或实现类方法继承,前提是该方法及其参数本身没有任何 JAX-RS 注释。超类上的注解优先于已实现接口上的注解。如果子类或实现方法有任何 JAX-RS 注释,则忽略超类或接口方法上的所有注释。例如:
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:
在上面,ActivityLog.getFeed
继承了接口的@GET
和@Produces
注解。反过来:
@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
上面的@GET
注解ReadOnlyAtomFeed.getFeed
并没有被继承ActivityLog
.getFeed