users@jersey.java.net

Re: [Jersey] Design Question / Sub Resources

From: Wilhelmsen Tor Iver <TorIverW_at_arrive.no>
Date: Tue, 19 May 2009 14:34:35 +0200

> public BranchResource getBranchResource(@PathParam("id") final String
> headOfficeId) {
> return new BranchResource(headOfficeId); }
>
> Is there a better more JAX-RS/Jersey way of approaching this ?

When you have sub-resources that also can be top-level resources (and
you do not care about the parent relationship anymore after getting that
resource), perhaps try using an UriBuilder; then you construct the
resource links in the list of branches for a head office:

        @Path("branches")
        public List<BranchSummary> getBranchList(@Context UriInfo info)
{
                // Get all relevant branches under an office
                // and for each:
                URI link =
info.getBaseUriBuilder().path(BranchResource.class).path("{id}").build(a
BranchId);
                // Put link into list object
        }

@Path("/branch")
public class BranchResource {

        @Path("{id}")
        public BranchDetailResource getDetails() {
        }

        // ...
}