users@jersey.java.net

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

From: Gili <cowwoc_at_bbs.darktech.org>
Date: Mon, 20 Jun 2011 05:24:28 -0700 (PDT)

Hi Cameron,

     I would like to differentiate between the different devices based
on the Content-Type but the following code seems a bit weird:

@Consumes("Dog")
@Produces("Dog")
public DogResource getDog()
{
   return new DogResource();
}

@Consumes("Cat")
@Produces("Cat")
public CatResource getCat(){
   return new CatResource();
}

[...]

DogResource
{
   @GET
   public String getDog() {}
   @PUT
   public void setDog(String) {}
}

CatResource
{
   @GET
   public String getCat() {}
   @PUT
   public void setCat(String) {}
}

     What do you think?

Gili

On 20/06/2011 8:04 AM, Cameron Heavon-Jones [via Jersey] wrote:
> 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.
>
>
>
> ------------------------------------------------------------------------
> If you reply to this email, your message will be added to the
> discussion below:
> http://jersey.576304.n2.nabble.com/Specifying-different-sub-resources-depending-on-the-Content-Type-tp6487299p6495488.html
>
> To unsubscribe from Specifying different sub-resources depending on
> the Content-Type?, click here
> <http://jersey.576304.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=6487299&code=Y293d29jQGJicy5kYXJrdGVjaC5vcmd8NjQ4NzI5OXwxNTc0MzIxMjQ3>.
>



--
View this message in context: http://jersey.576304.n2.nabble.com/Specifying-different-sub-resources-depending-on-the-Content-Type-tp6487299p6495551.html
Sent from the Jersey mailing list archive at Nabble.com.