GZIP encoding in Jersey 2 / Grizzly

前端 未结 2 1111
天命终不由人
天命终不由人 2020-12-16 20:54

I can\'t activate gzip-encoding in my Jersey service. This is what I\'ve tried:

  1. Started out with the jersey-quickstart-grizzly2 archetype from t

相关标签:
2条回答
  • 2020-12-16 21:35

    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.

    0 讨论(0)
  • 2020-12-16 21:39

    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();
    
    0 讨论(0)
提交回复
热议问题