【问题标题】:Unable to Run my Filter implemented with ContainerResponseFilter无法运行使用 ContainerResponseFilter 实现的过滤器
【发布时间】:2013-07-27 01:46:29
【问题描述】:

我正在尝试通过通用过滤器解析 GET 响应,并根据收到的内容向客户端发送响应。但我无法执行 Filter 类。我在下面添加了代码 sn-p。任何帮助将不胜感激

@FrontierResponse
@Provider
public class PoweredbyResponseFilter implements ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
        throws IOException {

    if (requestContext.getMethod().equals("GET")) {
        System.out.println("hellos");
    }

    //responseContext.getHeaders().add("X-Powered-By", "Jersey :-)");
}

}

我的自定义注解

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@NameBinding
public @interface FrontierResponse {
}

这是我的资源类

@GET
@FrontierResponse
@Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON})
public List<String> list() {...}

我正在使用的其他信息

jersey jersey-bundle-1.17.jar javax.ws.rs-api-2.0.jar

【问题讨论】:

    标签: java jersey jax-ws jax-rs


    【解决方案1】:

    你需要用@FrontierResponse来装饰资源类的方法,而不是资源类。或者,如果您想对所有请求使用过滤器,您可以注释 Application 类的扩展。 Check spec for more details.

    编辑: @NameBinding 自 v2.0 起就在 JAX-RS 中。您需要使用该规范的实现,即 Jersey 2.x。在球衣 1.x 中,此注释将被忽略。

    【讨论】:

    • 其实这是一个资源类的“方法”注解,不在Resource类中,但过滤器类仍然没有被执行。
    • 这可能与我使用的球衣版本有关。球衣 1.17 和 javax.ws.rs-api-2.0,jar?会不会有库版本冲突?
    • 抱歉,我看到的是我想看的,不是写的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 2022-11-11
    • 2016-06-04
    • 2018-12-15
    • 2019-05-12
    相关资源
    最近更新 更多