直到 3.0 版,Java servlet API 才为 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();
最好的问候 乔臣
最佳答案
你不需要 HttpServletResponseWrapper
用于 GZIP 压缩。它可以通过 WriterInterceptor
来实现来自 JAX-RS:
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();
}
}
然后注册 WriterInterceptor
在你的ResourceConfig
/Application
子类:
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(GZIPWriterInterceptor.class);
}
}
要将拦截器绑定(bind)到某些资源方法或类,您可以使用 name binding annotations .
关于java - Jersey 2 状态代码在 HttpServletResponseWrapper 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46691209/