使用 JAX-RS 进行继承
2022-09-01 18:39:37
我正在用于我的 Web 服务。我有共同的功能,并希望使用继承。我正在提供简单的 CRUD 操作。我定义了一个这样的接口:JAX-RS
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);
}
我有一个实现此接口的抽象类:
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));
}
});
}
}
最后,我的实现类只是为资源提供了一个 PATH:
@Path("meeting")
public class MeetingRestServices extends BaseREST {
}
当我尝试访问我的资源时(假设上下文根是 /):
http://localhost:8080/webresources/meeting/retreive/0
我得到一个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);
}
}