craig
craig

Reputation: 26262

JAX-RS refactor sub-resource into separate Resource class?

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.

Upvotes: 3

Views: 5745

Answers (1)

migu
migu

Reputation: 1401

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

Upvotes: 3

Related Questions