users@jersey.java.net

[Jersey] Re: UriInfo injection at resource initialization time

From: Martin Matula <martin.matula_at_oracle.com>
Date: Wed, 09 Mar 2011 12:20:27 +0100

Hi Martynas,

On 9.3.2011 12:01, Martynas Jusevicius wrote:
> Hey Markus,
>
> thanks for your patience :)
>
>> Why is it so essential to inject at resource creation?
> Because that would seem like a normal object-oriented practice?
>
> UriInfo injection in constructor is used in an example in "Overview of
> JAX-RS 1.0 Features"
> http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features#OverviewofJAX-RS1.0Features-ConditionalGETsandReturning304%28NotModified%29Responses
> It also says "Notice that in this example the constructor of a
> resource class can be used perform actions that may otherwise have to
> be duplicated to invoked for each resource method".
> That makes perfect sense to me - but are you saying this wouldn't
> work in Jersey?
It does work, but only on request-scoped resources - which they are by
default. But since you added @Singleton annotation to your resource
class, it can't work. See the JAX-RS spec which talks about it.
> I see a good specification/implementation letting me be flexible with
> my code, instead of making me bend my code to fit it.
> I had implemented the same functionality with a simple HttpServlet, so
> I'm still not sure if the transition to JAX-RS is worth the trouble.
It is simple - you can't get request-specifc info outside of the request
context. Not sure what's confusing about it. UriInfo includes specific
URI that was used to make a particular request - as you know, resources
can be parameterized, so the URI is not always known at the deploy time.

Anyway, seems what you are looking for is not request specific - you are
just trying to obtain that info in a request specific way. So, all you
need is doing it in a different way which is not request specific - see
my other e-mail...
Martin