users@jersey.java.net

Re: [Jersey] Re: Jersey 1.2 and Guice

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Mon, 30 Aug 2010 11:07:52 +0200

On Aug 30, 2010, at 12:13 AM, Alexandru Popescu ☀ wrote:

> 2010/8/29 Alexandru Popescu ☀ <the.mindstorm.mailinglist_at_gmail.com>:
>> Hi,
>>
>> I learn that there have been some improvements in the Guice
>> integration in the last Jersey release. Unfortunately I need to stay
>> with Jersey 1.2 due to the 1.5 requirement. Anyways, here are my two
>> questions:
>>
>> 1. are there any major reasons for the current Guice integration not
>> to work with Jersey 1.2?
>> 2. what exactly are the current limitations?
>>
>> All I could find about this have been published on Paul's blog, but
>> I'm worried if that is the complete information. For example, this
>> part is not at all clear: " Unfortunately due to Guice's restriction
>> in binding it is not currently possible to support the @*Params in
>> the
>> same manner. "
>>
>
> After digging through the code I just wanted to partially answer
> myself some of the questions:
>
> 1. it looks like using jersey-guice 1.3 with jersey 1.2 will not work
> out of box, mainly because of the new WebApplication.getProviders. On
> this note, I think it would be tremendously useful to use the @since
> javadoc to mark new API.
>

Good point. I will look into updating the JavaDoc.


> 2. while I think I understand how the integration works there are
> still a couple of questions:
>
> Guice seems to be configured to server everything through
> GuiceContainer. That raises the questions:
>
> - how can you bypass Jersey for some requests (say static resources,
> etc.)
> - how can you configure Jersey template engine
>
> My feeling is that this is still possible by replacing in
> GuiceServletContextListener:
>
> serve("/*").with(GuiceContainer.class)
>
> with:
>
> serve("/*").with(GuiceContainer.class, /* Map<String, String>*/
> params)
>
> where params will contain the configuration that you'd otherwise pass
> to ServletContainer
>
> Can anyone confirm this?
>

You will need to use a filter. For Jersey 1.4-ea05 or above can do this:

                 Map<String, String> params = new HashMap<String,
String>();
                 params.put(PackagesResourceConfig.PROPERTY_PACKAGES,
"com.foo");
                  
params.put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, "true");

                 filter("/*").through(GuiceContainer.class, params);

For earlier releases you can use a regex with:

https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html
#PROPERTY_WEB_PAGE_CONTENT_REGEX

Please see a recent thread between myself and Bill on using Jersey
with Guice and templates:

http://markmail.org/search/?q=list
%3Anet.java.dev.jersey.users#query:list%3Anet.java.dev.jersey.users
+page:1+mid:yugcjooukrhory5j+state:results

You will also find an example in that thread. Unfortunately there are
some bugs in Guice 2.0 that limit the behavior.


> 3. A part that is still unclear is Paul's quote above related to
> @*Params. If someone would be able to clarify it, I think that would
> be great.
>

Jersey cannot integrate fully with Guice to support the injection of
@*Param in the constructors. I tried to integrate as much as possible
with the JerseyServletModule in 1.3 so that @Inject can work with some
JAX-RS and Jersey types.

The current way Guice defines bindings makes it impossible to support
@*Param in conjunction with @Inject. I have some ideas on how to
modify Guice to support such "looser" bindings, but i am struggling to
find the time to dive and provide a patch.


> 4. A new question that arose while looking at the jersey-guice in 1.3
> and the sample code is: who is actually injecting the values.
>
> The PerRequestResource is defined as:
>
> [code]
> @Path("bound/perrequest")
> @RequestScoped
> public class PerRequestResource {
>
> @Context UriInfo ui;
>
> @QueryParam("x") String x;
>
> private final SingletonComponent sc;
>
> @Inject
> public PerRequestResource(SingletonComponent sc) {
> this.sc = sc;
> }
>
> }
> [/code]
>
> While it is clear that Guice will manage the lifecycle of this
> resource (per request) and will inject the SingletonComponent through
> the constructor, it is not clear who will provide @Context UriInfo and
> @QueryParam("x") String
>

Guice will inject everything related to @Inject. Jersey will inject
everything related to the JAX-RS annotations. Basically after Guice
instantiates and injects Jersey will have a go injecting.


> I know this mail got really long, so I apologize in advance for the
> effort to go through it while still hoping to get some help from the
> more experienced people on the list.
>

Np, hth,
Paul.