users@jersey.java.net

[Jersey] Spring managed providers

From: Cédric Thiébault <cedric.thiebault_at_gmail.com>
Date: Thu, 14 Nov 2013 11:51:16 -0500

I’m using Jersey 2.4.1, Spring 3.2.4 and Jetty.
My classes annotated with @Provider are also annotated @Component because I
need injection.
I’m testing with:

   - a ContainerRequestFilter (annotated with @Provider and @Component)
   - an ExceptionMapper (annotated with @Provider and @Component)
   - a resource (annotated with @Path and @Component)

I’ve configured Jersey to scan my packages with these providers.
When I access the resource, the injection in this resource is fine but my
request never go threw the ContainerRequestFilter.
It seems that Jersey did not registered it…. even if it appears in the logs:
Spring managed bean, filterTest, registered with HK2.

Did I miss something in my configuration of Jersey with Spring?

Here is my config.

ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.packages("org.obiba.jta.web");
resourceConfig.register(RequestContextFilter.class);
resourceConfig.property(ServletProperties.PROVIDER_WEB_APP, "true");
servletContextHandler.addServlet(new ServletHolder(new
ServletContainer(resourceConfig)), "/*");

Here are the logs on startup:

INFO - org.glassfish.jersey.server.spring.SpringComponentProvider -
Spring context lookup done.
INFO - org.glassfish.jersey.server.spring.SpringComponentProvider -
Spring component provider initialized.
INFO - org.glassfish.jersey.server.spring.SpringComponentProvider -
Spring managed bean, testResource, registered with HK2.
INFO - org.glassfish.jersey.server.spring.SpringComponentProvider -
Spring managed bean, nullPointerExceptionMapper, registered with HK2.
INFO - org.glassfish.jersey.server.spring.SpringComponentProvider -
Spring managed bean, filterTest, registered with HK2.
[...]
INFO - org.glassfish.jersey.server.ApplicationHandler - Jersey
application initialized.
Root Resource Classes:
  org.obiba.jta.web.TestResource
Pre-match Filters:
   org.glassfish.jersey.filter.LoggingFilter
Global Response Filters:
   org.glassfish.jersey.filter.LoggingFilter
Global Reader Interceptors:
   org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor
Global Writer Interceptors:
   org.glassfish.jersey.filter.LoggingFilter

I’ve also posted this question on stackoverflow with a slightly different
config:
http://stackoverflow.com/questions/19962472/jersey-spring-managed-providers

Thanks in advance!

Cedric