Java 泽西响应滤波器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21251674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:40:22  来源:igfitidea点击:

Jersey Response Filter

javafilterjerseyservlet-filtersjersey-2.0

提问by user3218089

I am trying to create a jersey filter that filters all the server's responses of 500 error.

我正在尝试创建一个球衣过滤器来过滤所有服务器的 500 错误响应。

But I dont know much about filters so I just started some code but I have no idea how to continue...can anybody help me with this issue.

但我对过滤器了解不多,所以我刚开始写一些代码,但我不知道如何继续……有人能帮我解决这个问题吗?

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
}

and my web.xml looks like this

我的 web.xml 看起来像这样

<servlet>
<servlet-name>Authenticator</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
    <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
    <param-value>com.query.displayer.Filters</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Authenticator</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

采纳答案by Michal Gajdos

You're mixing 2 versions of Jersey in your application: Implementation of a ContainerRequestFilterfrom JAX-RS 2.0 (Jersey 2) and descriptor configuration (web.xml) for Jersey 1 (see prefix com.sun.jersey). The following article explains how to register providers in Jersey 2:

您在应用程序中混合了 2 个 Jersey 版本:JAX-RS 2.0 (Jersey 2)的ContainerRequestFilter 的实现和 Jersey 1 的描述符配置 (web.xml)(请参阅前缀com.sun.jersey)。以下文章解释了如何在 Jersey 2 中注册提供者:

It seems that you we're using ResourceFilterFactoryin Jersey 1. This concept doesn't exist in Jersey 2 but there is a new concept (directly in JAX-RS 2.0) how to do that:

我们似乎ResourceFilterFactory在 Jersey 1 中使用了您。 Jersey 2 中不存在此概念,但有一个新概念(直接在 JAX-RS 2.0 中)如何做到这一点:

回答by Vitorlui

When you are using Jersey-2 you must use the follow configuration to register your filter into the web.xml

当您使用 Jersey-2 时,您必须使用以下配置将过滤器注册到 web.xml 中

jersey.config.server.provider.classnames

jersey.config.server.provider.classnames

instead of

代替

com.sun.jersey.spi.container.ContainerRequestFilters(jersey-1x)

com.sun.jersey.spi.container.ContainerRequestFilters(jersey-1x)

 <!-- This is the config needed -->
<servlet>    
      //...         
     <init-param>
         <param-name>jersey.config.server.provider.classnames</param-name>
         <param-value>com.your_package_path.yourClassFilter</param-value>
     </init-param>
      //...
</servlet>