users@jersey.java.net

[Jersey] Re: Specifying different sub-resources depending on the Content-Type?

From: Cameron Heavon-Jones <cmhjones_at_gmail.com>
Date: Mon, 20 Jun 2011 13:03:20 +0100

it sounds like you want a differentiator to be able to choose between the types of resources.

you will either require the client to provide the differentiator as part of their request - uri, media type, headers etc - the switch can be handled by jersey using annotations.

or you can store locally in your database and lookup the correct type on request. in your case this would amount to loading the device from the database and checking it's differentiator:

@Path("device/{id}")
public DeviceResource getDevice(@PathParam("id") Long id){
        Device d = loadDevice(id);
        if (d.isSomeType()) // or instanceof with cast
                return new SpecificDeviceResource(device);
        else
                return new GenericDeviceResource(device);
}


The other method, to get this managed by the client instead, you could do:

@Path("genericdevice/{id}")
public DeviceResource getGeneric(@PathParam("id") Long id){
        return new GenericDeviceResource(id);
}

@Path("specificdevice/{id}")
public DeviceResource getSpecific(@PathParam("id") Long id){
        return new SpecificDeviceResource(id);
}

or use some other request parameter other than the path.

hth,
cam

On 17/06/2011, at 3:23 PM, Gili wrote:

> Hi,
>
> Currently when I want to specify sub-resource I do:
>
> @Path("devices")
> class Devices
> {
> [...]
> @Path("{id}")
> public getDevice(@PathParam("id") long id)
> {
> return new Device(id);
> }
> }
>
> Now I have two different kind of devices, both mapped under /devices/{id}
> except that some IDs are mapped to one device type and some to another. Is
> it possible to specify two sub-resource methods and have Jersey instantiate
> a different sub-resource Class depending on the content-type being
> requested?
>
> Thanks,
> Gili
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Specifying-different-sub-resources-depending-on-the-Content-Type-tp6487299p6487299.html
> Sent from the Jersey mailing list archive at Nabble.com.