JAX-RS将子资源重构为单独的Resource类? [英] JAX-RS refactor sub-resource into separate Resource class?

查看:90
本文介绍了JAX-RS将子资源重构为单独的Resource类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有子资源的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代码移至单独的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();   

        }  
}

在编译代码时,资源路由不起作用.

解决方案

如果要在单独的类中处理子资源,则必须在主资源类中省略该方法的请求方法标识符.

尝试为您的受信任会话创建子类.像这样在主资源类中返回该类的实例:

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

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.

解决方案

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

这篇关于JAX-RS将子资源重构为单独的Resource类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆