users@jersey.java.net

[Jersey] Re: UriInfo injection at resource initialization time

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Fri, 11 Mar 2011 17:01:42 +0100

On 03/11/2011 04:40 PM, Martynas Jusevicius wrote:
> It seems that only the root resource gets context initialized, not the
> subresource - even though they extend the same superclass which holds
> the injected field, so I would expect the same behaviour. And I mean
> request methods here, not constructors. The output:
>
> Resource: RootResource_at_fdde57 Method: @GET ServletContext:
> org.apache.catalina.core.ApplicationContextFacade_at_12cb562
> Resource: SubResource_at_150d191 Method: @GET ServletContext: null
>
> What am I missing again? Is it the same issue as here:
> http://jersey.576304.n2.nabble.com/Instatiating-subResources-with-Context-class-fields-td5360153.html
>
> Thanks again,
>
> Martynas

Yes, it's basically the same issue.

If you are directly intantiating the subresource using new SubResource() the subresource fields don't get injected. In
Jersey you can use ResourceContext.getResource(SubResource.class)[1] instead of direct constructor. That way the
subresource should get injected too.

FYI, in JAX-RS 2.0 we are considering to provide a standard resolution to this issue. See here:
http://java.net/projects/jax-rs-spec/lists/jsr339-experts/archive/2011-03/message/53

Marek

[1] http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/core/ResourceContext.html