users@jersey.java.net

RE: [Jersey] Inheriting _at_Path Question

From: Rabick, Mark A (MS) <"Rabick,>
Date: Thu, 5 Mar 2009 18:01:06 -0600

That's basically what I currently have (sub-resource locator) but I was
Just trying to see if there was a way to get resource class-level
attribute
@Context injection into the sub-resource. I can only get injected
@Context
Resources in the sub-resource if I put the injections in the parameter
list of
Resource methods or pass the injected attributes to the sub-resource in
the
Constructor invocation... Like:

> @Path("v1")
> public class V1BaseResource {
>
> @Context protected UriInfo uriInfo;
>
> @Path("node")
> public V1NodeResource getNode() {
> return new V1NodeResource(uriInfo);
> }
> }
>
> public class V1NodeResource {
> private UriInfo uriInfo
> public V1NodeResource(UriInfo ui) {
> this.uriInfo = ui;
> }
>
> @GET @Path("/{key}")
> @Produces(MediaType.APPLICATION_XML)
> public Node getNodeByKey( @PathParam("key") String key ) {
> return NodeDB.getNodeByKey(key);
> }
> }

_______________________________________________
Mark A. Rabick - Software Engineer
Em: mark.rabick_at_ngc.com

 

> -----Original Message-----
> From: Marc.Hadley_at_Sun.COM [mailto:Marc.Hadley_at_Sun.COM]
> Sent: Thursday, March 05, 2009 5:54 PM
> To: users_at_jersey.dev.java.net
> Subject: Re: [Jersey] Inheriting @Path Question
>
> On Mar 5, 2009, at 5:46 PM, Rabick, Mark A (MS) wrote:
> > Is there a way to inherit pathing from superclasses of a resource?
> >
> > Ie.
> >
> > <Base Class>
> > @Path("/v1")
> > public class V1BaseResource {
> > @Context
> > protected UriInfo uriInfo;
> > }
> > </Base Class>
> >
> > <Sub Class>
> > @Path("/node")
> > public class V1NodeResource extends V1BaseResource {
> >
> > @GET @Path("/{key}")
> > @Produces(MediaType.APPLICATION_XML)
> > public Node getNodeByKey( @PathParam("key") String key ) {
> > return NodeDB.getNodeByKey(key);
> > }
> > }
> > </Sub Class>
> >
> > Where the URI would be like:
> >
> > http://localhost:7001/rest/root/v1/node/11111
> >
> > That would allow all methods in the V1NodeResource
> sub-class to have
> > the shared URI root of "v1".
> >
> > Is such a thing possible?
> >
>
> You need to use sub resource locators rather than inheritance
> for that, e.g.
>
> @Path("v1")
> public class V1BaseResource {
>
> @Context protected UriInfo uriInfo;
>
> @Path("node")
> public V1NodeResource getNode() {
> return new V1NodeResource(uriInfo);
> }
> }
>
> public class V1NodeResource {
> @GET @Path("/{key}")
> @Produces(MediaType.APPLICATION_XML)
> public Node getNodeByKey( @PathParam("key") String key ) {
> return NodeDB.getNodeByKey(key);
> }
> }
>
> Now when you GET
> http://localhost:7001/rest/root/v1/node/11111, Jersey will
> match the request to V1BaseResource for the initial "v1" path
> segment and then call the getNodeMethod since it matches the
> next "node" path segment. The object returned by
> getNodeMethod will then be treated as a resource class for
> the remaining part of the path "11111"
> so it will call getNodeByKey("11111").
>
> Note that there's no @Path on the V1NodeResource class, it
> can only be reached through a matching sub resource locator
> (V1BaseResource.
> getNode).
>
> HTH,
> Marc.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
>