GZIP encoding in Jersey 2 / Grizzly

一曲冷凌霜 提交于 2019-11-29 04:02:42

Try the code like:

HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
        BASE_URI, rc, false);

CompressionConfig compressionConfig =
        httpServer.getListener("grizzly").getCompressionConfig();
compressionConfig.setCompressionMode(CompressionConfig.CompressionMode.ON); // the mode
compressionConfig.setCompressionMinSize(1); // the min amount of bytes to compress
compressionConfig.setCompressableMimeTypes("text/plain", "text/html"); // the mime types to compress

httpServer.start();

You have to register the org.glassfish.jersey.server.filter.EncodingFilter as well. This example enables deflate and gzip compression:

import org.glassfish.jersey.message.DeflateEncoder;
import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.filter.EncodingFilter;
...
private void enableCompression(ResourceConfig rc) {
    rc.registerClasses(
            EncodingFilter.class,
            GZipEncoder.class,
            DeflateEncoder.class);
}

This solution is jersey specific and works not only with Grizzly, but with the JDK Http server as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!