users@jersey.java.net

Retrieving list of items that belong to another item

From: John O'Conner <john_at_joconner.com>
Date: Sun, 25 Jan 2009 05:01:15 -0800

Hi All,

I have an AccountsResource defined with these REST endpoints:
/accounts
/accounts/{id} -- AccountsResource returns an AccountResource

Also I have a GatewaysResource with these endpoints:
/gateways
/gateways/{id}

Accounts have gateways. Now what I'd like to do is define an endpoint that
provides all the gateways of a given account, maybe like this:
/accounts/{id}/gateways

With Jersey, do I simply add another method to the AccountsResource.java
file like this? I think this uri seems reasonable if I want the list of
gateways for a particular account, especially since a list of gateway ids is
in fact a property in my Account class:

@Path({id}/gateways)
public List<Gateway> getGateways(@PathParam("id") long id) {
    List<Gateway> gateways = null;
    // now perform the db query for this list
     return gateways;
}

Or maybe this belongs in AccountResource instead like this?
@Path("/gateways")
public List<Gateway> getGateways() { ...

I noticed that @Path examples often show a "resource" class as the return
value....but in my case above its just the list of actual objects. Is that
ok?

I have a GatewaysResource already that is providing a list of all gateways
in the system. Can I reuse that instead? Can I return a GatewaysResource
from an AccountsResource or from a AccountResource?

Thanks,
John