gpt4 book ai didi

java - 使用 JAX-RS 继承

转载 作者:IT老高 更新时间:2023-10-28 20:45:06 25 4
gpt4 key购买 nike

我正在为我的网络服务使用 JAX-RS。我有共同的功能,想使用继承。我提供简单的 CRUD 操作。我已经定义了一个这样的接口(interface):

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);
}

我有一个实现这个接口(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));
}
});
}

}

最后,我的实现类只是为资源提供了一个 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);
}

}

最佳答案

你上面描述的看起来不错。以下是 JAX-RS 继承规则,这些规则基于您提供的内容。

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() {...}
}

在上面,ActivityLog.getFeed从接口(interface)继承了@GET@Produces注解。反之:

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

在上面,ReadOnlyAtomFeed.getFeed上的@GET注解不是被ActivityLog继承的
.getFeed

关于java - 使用 JAX-RS 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25916796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com