繁体   English   中英

Jersey 2 状态代码在 HttpServletResponseWrapper 中不可见

[英]Jersey 2 status code not visible in HttpServletResponseWrapper

Java servlet API 直到 3.0 版才为 HttpServletResponse 提供 getStatus 方法。 我创建了一个带有 getStatus 的 HttpServletResponseWrapper 来包装 HttpServletResponse 并在设置时捕获状态。

这不适用于我的 Jersey 2 servlet。

我的 HttpServletResponseWrapper 是通过我的过滤器的 doFilter(request, wrapperResponse) 传递的。 当 Jersey RESTful Servlet 是端点时,会调用 Filter 但不会调用 getStatus 方法。

有没有我错过的配置?

我使用响应构建器返回结果并设置状态。

Response.status(404).build(); Response.status(200).type(mediaType).entity(theEntity).build();

最好的问候 Jochen

您不需要用于 GZIP 压缩的HttpServletResponseWrapper 它可以通过 JAX-RS 的WriterInterceptor实现:

public class GZIPWriterInterceptor implements WriterInterceptor {

    @Override
    public void aroundWriteTo(WriterInterceptorContext context)
                throws IOException, WebApplicationException {
        final OutputStream outputStream = context.getOutputStream();
        context.setOutputStream(new GZIPOutputStream(outputStream));
        context.proceed();
    }
}

然后在您的ResourceConfig / Application子类中注册WriterInterceptor

@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        register(GZIPWriterInterceptor.class);
    }
}

要将拦截器绑定到某些资源方法或类,您可以使用名称绑定注释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM