title: Spring + Jersey + Filter + @Provider
body: <p>I tried to configure Jersey with Spring using just annotations. I annotated my resource classes with <code>@Path</code> and <code>@Component</code> annotations. Everything worked fine until I tried to configure some filters. I created filter like this:</p>
  
  <pre><code>@Service
  @Provider @PreMatching
  public class MyFilter implements ContainerRequestFilter {
  
      public MyFilter() {
          System.out.println("MyFilter()");
      }
  
      @Override
      public ContainerRequest filter(ContainerRequest request) {
          System.out.println("filtered");
          return request;
      }
  }
  </code></pre>
  
  <p>It looks like bean is created and filter is registered in Jersey as when calling the resource I see: </p>
  
  <pre><code>INFO: Registering Spring bean, myFilter, of type pl.igt.filter.MyFilter as provider class
  </code></pre>
  
  <p>next to info about registration of resource components. 
  I can call my resources and it works fine, but filter is never used actually.</p>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
      &lt;display-name&gt;IGT&lt;/display-name&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
              classpath:applicationContext.xml
          &lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/listener-class&gt;
      &lt;/listener&gt; 
  
      &lt;!-- jersey 1.x --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;jersey-servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;com.sun.jersey.spi.spring.container.servlet.SpringServlet&lt;/servlet-class&gt;
          &lt;!-- 
          &lt;init-param&gt;
              &lt;param-name&gt;com.sun.jersey.spi.container.ContainerRequestFilters&lt;/param-name&gt;
              &lt;param-value&gt;pl.igt.filter.MyFilter&lt;/param-value&gt;
          &lt;/init-param&gt;
           --&gt;
      &lt;/servlet&gt;
  
      &lt;!-- jersey 2.x 
      &lt;servlet&gt;
          &lt;servlet-name&gt;jersey-servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
              &lt;param-value&gt;pl.igt.rest.JerseyApplication&lt;/param-value&gt;            
          &lt;/init-param&gt;       
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;  
      --&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;jersey-servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
          &lt;/welcome-file-list&gt;
      &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>applicationContext.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="pl.igt.rest" /&gt;
      &lt;context:component-scan base-package="pl.igt.filter" /&gt;
  &lt;/beans&gt;
  </code></pre>