Spring + Jersey + Filter + @Provider [英] Spring + Jersey + Filter + @Provider
问题描述
@Path
和 @Component
注释来注释我的资源类。一切正常,直到我试图配置一些过滤器。我创建了这样的过滤器:$ b $ @ $ $ $ $ b $提供@PreMatching
公共类MyFilter实现ContainerRequestFilter {
$ b $ public MyFilter(){
System.out.println(MyFilter());
$ b @Override
public ContainerRequest filter(ContainerRequest request){
System.out.println(filtered);
返回请求;
$ b $ p
$ b看起来像bean被创建并且过滤器被注册Jersey在调用资源的时候我看到:
pre $
INFO:注册类型为pl.igt.filter.MyFilter的Spring bean myFilter作为提供者类
作为资源组件注册信息旁边的提供者类。
我可以调用我的资源,它可以正常工作,但过滤器从来没有用过。
web.xml:
<?xml version =1.0encoding =UTF-8?>
< web-app version =2.5xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi: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>
<! - 球衣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>
<! - 运动衫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>
<加载启动> 1< /加载启动>
< / 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.0encoding =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>
解决方案,这里是解决方案,我已经结束:
为:
- Spring v3.2.9
- 泽西岛v1.18.1
$ b过滤器实现
package com.acme.sample.rest;
public class MyFilter implements ContainerRequestFilter {
$ b @Override
public ContainerRequest filter(ContainerRequest request){
System.out.println(filtered);
返回请求;
code
$ b web.xml
/ p> <?xml version =1.0encoding =UTF-8?>
< web-app version =2.5xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi: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>
<! - 球衣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< /参数值>
< / init-param>
<加载启动> 1< /加载启动>
< / servlet>
< servlet-mapping>
< servlet-name> jersey-servlet< / servlet-name>
< url-pattern> / rest / *< / url-pattern>
< / servlet-mapping>
< / web-app>
applicationContext.xml
<?xml version =1.0encoding =UTF-8?>
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :context =http://www.springframework.org/schema/contextxmlns: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/上下文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>
提供程序类
package com.acme.sample;
@Component
@Path(/ sample)
public class ApiProvider {
//添加bean的包来扫描applicationContext.xml文件
@Autowired
private SomeBean someBean;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path(/ healthy)
public Response getHealthy(){
String output ={\status \:\UP \};
返回Response.status(Response.Status.OK)
.entity(输出)
.build();
code
$ b pt.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd>
4.0.0
rest-provider
< name> jersey-servlet< / name>
< parent>
< groupId> com.acme.sample< / groupId>
< artifactId> jersey-servlet< / artifactId>
< version> 0.0.1 -snapshot< / version>
< / parent>
< packaging> war<包装>
<属性>
< jersey.version> 1.18.1< / jersey.version>
< spring.version> 3.2.9< /spring.version>
$ b $ dependency>
<! - Spring - >
<依赖项>
< groupId> org org .springframework< / groupId>
< artifactId> spring< / artifactId>
< / dependency>
< de未决>
< groupId> org.springframework< / groupId>
< artifactId> spring-aop< / artifactId>
< / dependency>
<依赖>
< groupId> org.springframework< / groupId>
< artifactId> spring-core< / artifactId>
< / dependency>
<依赖>
< groupId> org.springframework< / groupId>
< artifactId> spring-web< / artifactId>
< / dependency>
<依赖>
< groupId> org.springframework< / groupId>
< artifactId> spring-beans< / artifactId>
< / dependency>
<依赖>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< / dependency>
<! - 泽西岛 - >
<依赖>
< groupId> com.sun< / groupId>
< artifactId>球衣核心< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<依赖>
< groupId> com.sun< / groupId>
< artifactId> jersey-json< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<依赖>
< groupId> com.sun< / groupId>
< artifactId>球衣服务器< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<依赖>
< groupId> com.sun< / groupId>
jersey-servlet< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<! - Jersey + Spring - >
<依赖>
< groupId> com.sun.jersey.contribs< / groupId>
< artifactId>球衣弹簧< / artifactId>
< version> $ {jersey.version}< / version>
< scope>提供< / scope>
<排除>
<排除>
< groupId> org.springframework< / groupId>
< artifactId> spring< / artifactId>
< /排除>
<排除>
< groupId> org.springframework< / groupId>
< artifactId> spring-aop< / artifactId>
< /排除>
<排除>
< groupId> org.springframework< / groupId>
< artifactId> spring-core< / artifactId>
< /排除>
<排除>
< groupId> org.springframework< / groupId>
< artifactId> spring-web< / artifactId>
< /排除>
<排除>
< groupId> org.springframework< / groupId>
< artifactId> spring-beans< / artifactId>
< /排除>
<排除>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< /排除>
< /排除>
< / dependency>
< /依赖关系>
< build>
< finalName> jersey-servlet< / finalName>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-war-plugin< / artifactId>
< version> 2.6< / version>
<配置>
<档案>
< manifest>
< addClasspath> true< / addClasspath>
< / manifest>
< manifestFile> src / main / webapp / META-INF / MANIFEST.MF< / manifestFile>
< / archive>
< / configuration>
< / plugin>
< / plugins>
< / build>
< / project>
I tried to configure Jersey with Spring using just annotations. I annotated my resource classes with @Path
and @Component
annotations. Everything worked fine until I tried to configure some filters. I created filter like this:
@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;
}
}
It looks like bean is created and filter is registered in Jersey as when calling the resource I see:
INFO: Registering Spring bean, myFilter, of type pl.igt.filter.MyFilter as provider class
next to info about registration of resource components.
I can call my resources and it works fine, but filter is never used actually.
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>
解决方案 For those who have came accross here and frustrated with no solution, here is the solution I have ended up with:
For:
- Spring v3.2.9
- Jersey v1.18.1
Filter Implementation
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 class
package 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
rest-provider
<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>
这篇关于Spring + Jersey + Filter + @Provider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!