users@jersey.java.net

Re: [Jersey] _at_Context annotation...

From: Martin Grotzke <martin.grotzke_at_freiheit.com>
Date: Fri, 11 Jul 2008 09:28:22 +0200

On Thu, 2008-07-10 at 10:19 +0200, Lars Tackmann wrote:
> On Wed, Jul 9, 2008 at 6:15 PM, Bob Namestka <Robert.Namestka_at_sun.com> wrote:
> > Hello All,
> >
> > I'm a relative Jersey noob. I'm using jersey-08 for my prototyping.
> >
> > Question: When using:
> >
> > @Context
> > private UriInfo context;
> >
> > inside of a (sub)Resource class, should that context be available anywhere
> > in the Resource chain ?
> >
> > In other words, given:
> >
> >
> > http://somewhare.com/myapp/users/user123/trinkets/sku456
> >
> > and have implemented classes for:
> > UsersResource, UserResource, TrinketsResource and TrinketResource
> >
> > following the pattern(s) in the jersey examples, and using:
> >
> > @Context
> > private UriInfo context;
> >
> > inside of TrinketResource, should context be non-null ?
>
> No @Context can only inject into resource classes that themselves is
> annotated with path (root resources). If you
> need it in a sub resource then you must explicitly pass it on in the
> constructor.
Alternatively you can get subresources from jersey using the
ResourceContext:

    @Path("/")
    public static class MyRootResource {
        
        @Context ResourceContext _resourceContext;
        
        @Path( "subresource" )
        public MySubResource getMySubResource() {
            MySubResource result = _resourceContext.getResource( MySubResource.class );
            return result;
        }
        
    }

Then the subresource will get all required fields injected.

Cheers,
Martin


>
> Here is a (root) resource that uses this technique:
> http://svn.randompage.org/java/samples/jax-rs/acegi/src/main/java/org/randompage/samples/jaxrs/acegi/resources/UsersResource.java
>
> here is the sub resource it uses
> http://svn.randompage.org/java/samples/jax-rs/acegi/src/main/java/org/randompage/samples/jaxrs/acegi/resources/UserResource.java
>
> Hope that helps.
>
>