use*_*041 21 java rest inheritance annotations jax-rs
我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)
我有一个实现此接口的抽象类:
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));
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我的实现类只为资源提供了一个PATH:
@Path("meeting")
public class MeetingRestServices extends BaseREST {
}
Run Code Online (Sandbox Code Playgroud)
当我尝试访问我的资源时(假设上下文根是/):
http://localhost:8080/webresources/meeting/retreive/0
Run Code Online (Sandbox Code Playgroud)
我得到了404,它说它找不到它.我的想法是在继承的某个地方,它正在弄乱我认为资源应该在哪里的路径.有什么想法吗?
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);
}
}
Run Code Online (Sandbox Code Playgroud)
cmd*_*cmd 41
你上面描述的内容看起来不错.以下是JAX-RS继承的规则,它基于您所提供的内容.
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() {...}
}
Run Code Online (Sandbox Code Playgroud)
在上面,从接口ActivityLog.getFeed继承@GET和@Produces注释.反过来:
@Path("feed")
public class ActivityLog implements ReadOnlyAtomFeed {
@Produces("application/atom+xml")
public Feed getFeed() {...}
}
Run Code Online (Sandbox Code Playgroud)
在上面,@GET注释on ReadOnlyAtomFeed.getFeed不是由继承的ActivityLog
.getFeed
| 归档时间: |
|
| 查看次数: |
13680 次 |
| 最近记录: |