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