2015-04-03 100 views
1

我試着用Spring配置Jersey來使用註釋。我用@Path@Component註釋說明了我的資源類。一切工作正常,直到我試圖配置一些過濾器。我創造了這樣的過濾器:Spring + Jersey + Filter + @Provider

@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; 
    } 
} 

它看起來像創建豆和過濾器是註冊於澤西呼喚我看到了資源時爲:

INFO: Registering Spring bean, myFilter, of type pl.igt.filter.MyFilter as provider class 

旁邊的有關資源組件的註冊信息。 我可以打電話給我的資源,它工作正常,但實際上從未使用過濾器。

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <display-name>IGT</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      classpath:applicationContext.xml 
     </param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.filter.DelegatingFilterProxy</listener-class> 
    </listener> 

    <!-- jersey 1.x --> 
    <servlet> 
     <servlet-name>jersey-servlet</servlet-name> 
     <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> 
     <!-- 
     <init-param> 
      <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> 
      <param-value>pl.igt.filter.MyFilter</param-value> 
     </init-param> 
     --> 
    </servlet> 

    <!-- jersey 2.x 
    <servlet> 
     <servlet-name>jersey-servlet</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>javax.ws.rs.Application</param-name> 
      <param-value>pl.igt.rest.JerseyApplication</param-value>    
     </init-param>  
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    --> 

    <servlet-mapping> 
     <servlet-name>jersey-servlet</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
     </welcome-file-list> 
    </web-app> 

的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <context:annotation-config /> 
    <context:component-scan base-package="pl.igt.rest" /> 
    <context:component-scan base-package="pl.igt.filter" /> 
</beans> 
+0

如果什麼你試試在你的pl.igt.rest.JerseyApplication註冊這個過濾器嗎? – mkrakhin 2015-04-03 11:07:27

+1

我不明白你爲什麼使用兩個不同的Jersey版本?那個過濾器甚至不能和Jersey 2一起工作,因爲它是一個Jersey 1過濾器。 [Spring 2支持](https://jersey.java.net/documentation/latest/spring.html) – 2015-04-03 11:12:41

+0

也請參閱[過濾澤西2](https://jersey.java.net/documentation/latest/filters-and-interceptors.html)爲正確的過濾器 – 2015-04-03 11:16:07

回答

0

對於那些誰也來到這裏防空火炮和無解沮喪,這裏是我已經結束了與解決方案:

For:

  • 春v3.2.9
  • 新澤西v1.18.1

濾波器的實現

package com.acme.sample.rest; 
public class MyFilter implements ContainerRequestFilter { 

    @Override 
    public ContainerRequest filter(ContainerRequest request) { 
     System.out.println("filtered"); 
     return request; 
    } 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <display-name>IGT</display-name> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- jersey 1.x --> 
    <servlet> 
     <servlet-name>jersey-servlet</servlet-name> 
     <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>com.acme.sample</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> 
      <param-value>com.acme.sample.rest.MyFilter</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>jersey-servlet</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

applicationContext.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<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:tx="http://www.springframework.org/schema/tx" 
    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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 

    <context:component-scan base-package="com.acme.sample" /> 
</beans> 

Provider類 包com.acme.sample;

@Component 
@Path("/sample") 
public class ApiProvider { 

    // Add the bean's package to package scan of applicationContext.xml file 
    @Autowired 
    private SomeBean someBean; 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    @Path("/healthy") 
    public Response getHealthy() { 

     String output = "{\"status\":\"UP\"}"; 
     return Response.status(Response.Status.OK) 
       .entity(output) 
       .build(); 
    } 
} 

的pom.xml http://maven.apache.org/xsd/maven-4.0.0.xsd「> 4.0.0 休息提供商

<name>jersey-servlet</name> 
    <parent> 
     <groupId>com.acme.sample</groupId> 
     <artifactId>jersey-servlet</artifactId> 
     <version>0.0.1-SNAPSHOT</version> 
    </parent> 
    <packaging>war</packaging> 
    <properties> 
     <jersey.version>1.18.1</jersey.version> 
     <spring.version>3.2.9</spring.version> 
    </properties> 
    <dependencies> 

     <!-- Spring --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
     </dependency>  
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
     </dependency> 

     <!-- Jersey --> 
     <dependency> 
      <groupId>com.sun</groupId> 
      <artifactId>jersey-core</artifactId> 
      <version>${jersey.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun</groupId> 
      <artifactId>jersey-json</artifactId> 
      <version>${jersey.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>${jersey.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun</groupId> 
      <artifactId>jersey-servlet</artifactId> 
      <version>${jersey.version}</version> 
     </dependency> 

     <!-- Jersey + Spring --> 
     <dependency> 
      <groupId>com.sun.jersey.contribs</groupId> 
      <artifactId>jersey-spring</artifactId> 
      <version>${jersey.version}</version> 
      <scope>provided</scope> 
      <exclusions> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-aop</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-core</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-web</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-beans</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-context</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
    </dependencies> 
    <build> 
     <finalName>jersey-servlet</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.6</version> 
       <configuration> 
        <archive> 
         <manifest> 
          <addClasspath>true</addClasspath> 
         </manifest> 
         <manifestFile>src/main/webapp/META-INF/MANIFEST.MF</manifestFile> 
        </archive> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project>