我们在尝试在服务响应标头添加gzip内容编码时,使用 dropwizard-core:1.1.2 面临异常。详情如下:
GzipFilter.class
public class GzipFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Content-Encoding", "gzip");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
}
Service.class
@Override
public void run(DocumentServiceConfig config, Environment environment) throws Exception {
Injector injector = createInjector(config, environment);
environment.jersey().register(injector.getInstance(SomeResource.class));
environment.servlets().addFilter("Gzip-Filter", GzipFilter.class).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
config.yml
gzip:
enabled: true
minimumEntitySize: 256B
bufferSize: 32KB
针对500 API响应的异常堆栈跟踪 -
WARN [2017-08-04 00:48:20,713] org.eclipse.jetty.server.HttpChannel: /clients/v2 ! java.io.EOFException: null ! at java.util.zip.GZIPInputStream.readUByte(GZIPInputStream.java:268) ! at java.util.zip.GZIPInputStream.readUShort(GZIPInputStream.java:258) ! at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164) ! at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79) ! at io.dropwizard.jetty.BiDiGzipHandler.wrapGzippedRequest(BiDiGzipHandler.java:100) ! at io.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:64) ! at org.eclipse.jetty.server.handler.RequestLogHandler.handle(RequestLogHandler.java:56) ! at org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:169) ! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132) ! at org.eclipse.jetty.server.Server.handle(Server.java:564) ! at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320) ! at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251) ! at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279) ! at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110) ! at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124) ! at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:122) ! at org.eclipse.jetty.util.thread.strategy.ExecutingExecutionStrategy.invoke(ExecutingExecutionStrategy.java:58) ! at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:201) ! at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:133) ! at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672) ! at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590) ! at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
虽然我是否应该自己回答,但感到困惑。然而,由于更新的细节是半解决问题,因此我自己回答这个问题。
在此引用@arteam以提供当前实施的解决方案。
我相信Dropwizard会自动进行gzip压缩。支撑 默认情况下启用gzip(请参阅 http://www.dropwizard.io/1.1.2/docs/manual/configuration.html#gzip)。 因此,如果客户端通过发送请求支持解压缩
Accept-Encoding:gzip
标题,org.eclipse.jetty.server.handler.gzip.GzipHandler
会压缩 响应并添加Content-Encoding:gzip标头。
那么问题仍然存在,我仍然没有将此标记为这个问题的答案:
为什么您的自定义过滤器不起作用尚不清楚,可能您的过滤器在Jersey servlet之前执行并重写标题。
所以所需要的只是将service.yml
更改实现为:
gzip:
enabled: true
minimumEntitySize: 256B
bufferSize: 32KB
并且没有实现任何最终覆盖当前实现的CustomFilter,而不仅仅是覆盖但会导致标题异常。
需要注意的另一点是,应根据配置中指定的minimumEntitySize
的响应大小对其进行测试。