users@jersey.java.net

[Jersey] Re: UriInfo injection at resource initialization time

From: Martynas Jusevicius <martynas.jusevicius_at_gmail.com>
Date: Fri, 11 Mar 2011 18:04:00 +0100

Ok, I think I can work around it by adding

        if (parent != null) setServletContext(parent.getServletContext());

in the Resource() constructor.

On Fri, Mar 11, 2011 at 5:11 PM, Martynas Jusevicius
<martynas.jusevicius_at_gmail.com> wrote:
> I see. So I could do
>
>   @Path(SubResource.PATH)
>   public SubResource getSubResource(@Context UriInfo uriInfo)
>   {
>       return getResourceContext().getResource(SubResource.class);
>   }
>
> but SubResource constructor takes 2 params:
>
>   public SubResource(RootResource parent, @Context UriInfo uriInfo)
>
> How do I go about that?
>
> On Fri, Mar 11, 2011 at 5:01 PM, Marek Potociar
> <marek.potociar_at_oracle.com> wrote:
>> 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
>>
>