泽西过滤器的映射与另一个servlet冲突

时间:2014-09-21 09:38:51

标签: java spring servlets jersey mapping

在web.xml中有这样一个servlet:

<servlet>
        <servlet-name>Bla</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Bla</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

我有一个球衣过滤器和一个approp。 web.xml设置为:

<servlet>
        <servlet-name>MY API</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <param-value>BlaResponseFilter</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MY API</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

目的是将过滤器绑定到任何响应。

当然,由于多个servlet映射到同一路径(/ *)

,因此无效

另一种方法是什么?我需要在生成响应时运行特殊代码,但在将其发送给用户以获取任何请求之前。

1 个答案:

答案 0 :(得分:0)

部署描述符中com.sun.jersey.spi.container.ContainerRequestFilters int的值应该是实现com.sun.jersey.spi.container.ContainerRequestFilter接口的类,并且必须覆盖filter方法。

因此,提供价值似乎不合法。创建自己的过滤器:

package some.package.filter;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

public class BlaResponseFilter implements ContainerRequestFilter {  
 @Override
 public ContainerRequest filter(ContainerRequest request) {
    // you custom operations
  return request;
 }
}

然后登记:

<servlet>
    <servlet-name>MY_API</servlet-name><!-- Avoid space characters -->
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
        <param-value>some.package.filter.BlaResponseFilter</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MY_API</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>