0

在尝试在服务响应标头中添加 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)
4

1 回答 1

0

很困惑我是否应该自己回答这个问题。然而,由于更新的细节正在半解决问题,因此我自己回答了这个问题。

继续描述相同的Dropwizard#Issues#2126

在此处引用 @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 ,而不仅仅是覆盖而是导致标题异常。

Another point to note is, that this shall be tested against the response size of both more and less than the minimumEntitySize as specified in the configuration.

于 2017-08-28T16:44:44.707 回答