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>
如果什麼你試試在你的pl.igt.rest.JerseyApplication註冊這個過濾器嗎? – mkrakhin 2015-04-03 11:07:27
我不明白你爲什麼使用兩個不同的Jersey版本?那個過濾器甚至不能和Jersey 2一起工作,因爲它是一個Jersey 1過濾器。 [Spring 2支持](https://jersey.java.net/documentation/latest/spring.html) – 2015-04-03 11:12:41
也請參閱[過濾澤西2](https://jersey.java.net/documentation/latest/filters-and-interceptors.html)爲正確的過濾器 – 2015-04-03 11:16:07