JAX-RS 将子资源重构为单独的资源类?

发布于 2025-01-01 14:49:00 字数 1653 浏览 1 评论 0原文

具有子资源的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请持续率性 2025-01-08 14:49:00

如果要在单独的类中处理子资源,则必须省略主资源类中方法的请求方法指示符。

尝试为您的可信会话创建一个子类。在主资源类中返回此类的实例,如下所示:

@Path("/session")
public class SessionResource {
    // Note that the request method designator is omitted.
    @Path("/trusted")
    public TrustedSession getTrustedSession() {
        return new TrustedSession();
    }    
}

在子资源的类中,您只需注释请求方法:

public class TrustedSession {
    @POST
    @Produces("application/xml")
    public Response createTrustedSession() {
        URI uri = URI.create("/session/trusted/" + new Date().toString());
        return Response.created(uri).build();
    }

    @DELETE
    public Response destroySession() {
        return Response.noContent().build();
    }
}

子资源定位器(简要)解释于 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:

@Path("/session")
public class SessionResource {
    // Note that the request method designator is omitted.
    @Path("/trusted")
    public TrustedSession getTrustedSession() {
        return new TrustedSession();
    }    
}

In the class for the sub-resource, you just have to annotate the request methods:

public class TrustedSession {
    @POST
    @Produces("application/xml")
    public Response createTrustedSession() {
        URI uri = URI.create("/session/trusted/" + new Date().toString());
        return Response.created(uri).build();
    }

    @DELETE
    public Response destroySession() {
        return Response.noContent().build();
    }
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文