Jersey 2状态代码在HttpServletResponseWrapper中不可见
Java servlet API在版本3.0之前不为HttpServletResponse提供getStatus方法。我创建了一个包含getStatus的HttpServletResponseWrapper来封装HttpServletResponse并在设置时捕获状态。Jersey 2状态代码在HttpServletResponseWrapper中不可见
这不适用于我的Jersey 2 servlet。
我的HttpServletResponseWrapper通过我的过滤器的doFilter(request,wrapperResponse)传递。 Filter被调用,但当Jersey RESTful Servlet是端点时,getStatus方法不会被调用。
有没有我错过的配置?
我使用响应构建器来返回结果并设置状态。
Response.status(404).build(); Response.status(200).type(mediaType).entity(theEntity).build();
问候 约亨
你并不需要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);
}
}
要绑定拦截某些资源的方法或类,你可以使用name binding annotations。
得到它的工作。 WriterInterceptor只在我发送一个实体时才触发,因此我的404和204情况被覆盖了。但是如果请求没有Accept-Encoding,我怎么能跳过这个gzip:gzip,deflate,br header? – ScubaInstructor
@ScubaInstructor您应该可以使用'@Context HttpHeaders httpHeaders'在拦截器中注入请求标头。 –
我现在要用这个解决方案EncodingFilter.enableFor(this,GZipEncoder.class,DeflateEncoder.class);并跳过WriterInterceptor。 – ScubaInstructor
你需要一个'HttpServletResponseWrapper'来做什么? –
要获取gzip过滤器的状态码,请在404或204响应中不使用gzip标头。 – ScubaInstructor