JAX-RS 将子资源重构为单独的资源类?
具有子资源的 JAX-RS (Jersey) 资源类可以分为两个类吗?
目前,我将两者合并为一个资源类:
@Path("/session")
public class SessionResource {
@POST
@Produces("application/xml")
public Response createSession() {
...
ResponseBuilder builder = Response.created(URI.create("/session/" + new Date().toString()));
return builder.build();
}
@DELETE
public Response destroySession() {
...
ResponseBuilder builder = Response.noContent();
return builder.build();
}
// TrustedSession sub-resource
@POST
@Path("/trusted")
@Produces("application/xml")
public Response createTrustedSession() {
...
ResponseBuilder builder = Response.created(URI.create("/session/" + new Date().toString()));
return builder.build();
}
@DELETE
@Path("/trusted")
public Response destroyTrustedSession() {
...
ResponseBuilder builder = Response.noContent();
return builder.build();
}
}
我想将 TrustedSession 代码移至单独的资源:
@Path("/session/trusted")
public class createSession {
@POST
@Produces("application/xml")
public Response createTrustedSession() {
...
ResponseBuilder builder = Response.created(URI.create("/session/trusted/" + new Date().toString()));
return builder.build();
}
@DELETE
public Response destroySession() {
...
ResponseBuilder builder = Response.noContent();
return builder.build();
}
}
在代码编译时,资源路由不起作用。
Can a JAX-RS (Jersey) Resource class with a sub-resource be split into two classes?
Currently, I have the two combined into a single resource class:
@Path("/session")
public class SessionResource {
@POST
@Produces("application/xml")
public Response createSession() {
...
ResponseBuilder builder = Response.created(URI.create("/session/" + new Date().toString()));
return builder.build();
}
@DELETE
public Response destroySession() {
...
ResponseBuilder builder = Response.noContent();
return builder.build();
}
// TrustedSession sub-resource
@POST
@Path("/trusted")
@Produces("application/xml")
public Response createTrustedSession() {
...
ResponseBuilder builder = Response.created(URI.create("/session/" + new Date().toString()));
return builder.build();
}
@DELETE
@Path("/trusted")
public Response destroyTrustedSession() {
...
ResponseBuilder builder = Response.noContent();
return builder.build();
}
}
I would like to move the TrustedSession code to a separate Resouce:
@Path("/session/trusted")
public class createSession {
@POST
@Produces("application/xml")
public Response createTrustedSession() {
...
ResponseBuilder builder = Response.created(URI.create("/session/trusted/" + new Date().toString()));
return builder.build();
}
@DELETE
public Response destroySession() {
...
ResponseBuilder builder = Response.noContent();
return builder.build();
}
}
While the code compiles, the resource routing doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要在单独的类中处理子资源,则必须省略主资源类中方法的请求方法指示符。
尝试为您的可信会话创建一个子类。在主资源类中返回此类的实例,如下所示:
在子资源的类中,您只需注释请求方法:
子资源定位器(简要)解释于 Java EE 6 教程。
顺便说一句:使用 javax.ws.rs.core.UriBuilder(即使用其方法 fromResource(Class))可以更方便、更安全地构建 URI。
If you want to process a sub-resource in a separate class, you have to omit the request method designator for the method in the main resource class.
Try to create a sub class for your trusted session. Return an instance of this class in the main resource class like this:
In the class for the sub-resource, you just have to annotate the request methods:
Sub resource locators are (briefly) explained in The Java EE 6 Tutorial.
By the way: URIs can be build more conveniently and safely with the javax.ws.rs.core.UriBuilder, namely with its method fromResource(Class).