users@jersey.java.net

Re: [Jersey] Guice / Reference to WebApplication

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 04 Jun 2010 14:20:31 +0200

Hi,

I have just committed a fix to the trunk that adds support for JAX-RS
and Jeresy bindings. This may make it's way to a new 1.3-SNAPSHOT in a
couple of hours.

You can extend from com.sun.jersey.guice.JerseyServletModule (see
below) instead of ServletModule.

Tests have been added to ensure this works in production mode.


On Jun 4, 2010, at 1:50 PM, Johannes Schneider wrote:

>> There are some issues supporting injection of @*Param because of
>> restrictions in the Guice binding model. Modification of Guice will
>> be
>> required.
>
> Do you have sample code for this? Don't want to run into any
> problems....
>


You cannot currently do this:

  @Path("/foo")
  @RequestScoped
  public class Foo {
     @Inject @QueryParam("x") String x;
  }

  @Path("/bar")
  @RequestScoped
  public class Bar {
     @Inject @QueryParam("x") int x;
  }

Guice's binding approach is too restrictive. Guice's bindings have no
knowledge of their dependencies, which works well for 90% of the
cases. Jersey requires a modification to Guice such that a Dependency
[1] can be bound to a provider if that Dependency has a certain
annotation associated with it.

Paul.

[1] http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spi/Dependency.html


/**
  * A {_at_link ServletModule} module that provides JAX-RS and Jersey
related
  * bindings.
  * <p>
  * The module has a dependency on {_at_link GuiceContainer}, which is
required
  * to be served in the {_at_link ServletModule#configure() } method.
  * <p>
  * The following bindings are defined:
  * <ul>
  * <li>{_at_link WebApplication}
  * <li>{_at_link WebApplication}
  * <li>{_at_link Providers}
  * <li>{_at_link FeaturesAndProperties}
  * <li>{_at_link MessageBodyWorkers}
  * <li>{_at_link ExceptionMapperContext}
  * <li>{_at_link HttpContext}
  * <li>{_at_link UriInfo}
  * <li>{_at_link ExtendedUriInfo}
  * <li>{_at_link HttpRequestContext}
  * <li>{_at_link HttpHeaders}
  * <li>{_at_link Request}
  * <li>{_at_link SecurityContext}
  * <li>{_at_link HttpResponseContext}
  * </ul>
  * @author Paul.Sandoz_at_Sun.Com
  */
public class JerseyServletModule extends ServletModule {

     @Provides
     public WebApplication webApp(GuiceContainer guiceContainer) {
         return guiceContainer.getWebApplication();
     }

     @Provides
     public Providers providers(WebApplication webApplication) {
         return webApplication.getProviders();
     }

     @Provides
     public FeaturesAndProperties
fearturesAndProperties(WebApplication webApplication) {
         return webApplication.getFeaturesAndProperties();
     }

     @Provides
     public MessageBodyWorkers messageBodyWorkers(WebApplication
webApplication) {
         return webApplication.getMessageBodyWorkers();
     }

     @Provides
     public ExceptionMapperContext
exceptionMapperContext(WebApplication webApplication) {
         return webApplication.getExceptionMapperContext();
     }

     @RequestScoped
     @Provides
     public HttpContext httpContext(WebApplication webApplication) {
         return webApplication.getThreadLocalHttpContext();
     }

     @Provides
     @RequestScoped
     public UriInfo uriInfo(WebApplication wa) {
         return wa.getThreadLocalHttpContext().getUriInfo();
     }

     @Provides
     @RequestScoped
     public ExtendedUriInfo extendedUriInfo(WebApplication wa) {
         return wa.getThreadLocalHttpContext().getUriInfo();
     }

     @RequestScoped
     @Provides
     public HttpRequestContext requestContext(WebApplication wa) {
         return wa.getThreadLocalHttpContext().getRequest();
     }

     @RequestScoped
     @Provides
     public HttpHeaders httpHeaders(WebApplication wa) {
         return wa.getThreadLocalHttpContext().getRequest();
     }

     @RequestScoped
     @Provides
     public Request request(WebApplication wa) {
         return wa.getThreadLocalHttpContext().getRequest();
     }

     @RequestScoped
     @Provides
     public SecurityContext securityContext(WebApplication wa) {
         return wa.getThreadLocalHttpContext().getRequest();
     }

     @RequestScoped
     @Provides
     public HttpResponseContext responseContext(WebApplication wa) {
         return wa.getThreadLocalHttpContext().getResponse();
     }
}