2013-11-03 21 views
8

Jersey hizmetimde gzip kodlamasını etkinleştiremiyorum. Bu denedim budur: Jersey'de GZIP kodlaması 2/Grizzly

  1. Getting Started Guide den jersey-quickstart-grizzly2 arketip ile yola çıkıldı.

  2. Eklenen (aynı zamanda rc.register(org.glassfish.jersey.message.GZipEncoder.class); çalıştık)

  3. curl --compressed -v -o - http://localhost:8080/myapp/myresource

sonuç ile test edilmiştir mvn exec:java

  • ile başladı, aşağıdaki

    olan rc.register(org.glassfish.grizzly.http.GZipContentEncoding.class);:

    > GET /myapp/myresource HTTP/1.1 
    > User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 zlib/1.2.3.4 ... 
    > Host: localhost:8080 
    > Accept: */* 
    > Accept-Encoding: deflate, gzip 
    > 
    < HTTP/1.1 200 OK 
    < Content-Type: text/plain 
    < Date: Sun, 03 Nov 2013 08:07:10 GMT 
    < Content-Length: 7 
    < 
    * Connection #0 to host localhost left intact 
    * Closing connection #0 
    Got it! 
    

    Bu nedenle, Accept-Encoding: deflate, gzip isteğine rağmen yanıtta Content-Encoding: gzip yoktur.

    Burada neyim eksik? Siz de org.glassfish.jersey.server.filter.EncodingFilter kaydetmek zorunda

    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(); 
    
  • cevap

    8

    gibi kod deneyin.

    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); 
    } 
    

    Bu çözüm forması özeldir ve aynı zamanda Grizzly ile ancak JDK Http sunucu ile sadece çalışır: Bu örnek deflate ve gzip sıkıştırma sağlar.

    +0

    Hile yapmak gibi görünüyor! Grizzly 2.2.3'teydim, bu yüzden CompressConfig 'e sahip değildim, ancak NetworkListener'da ilgili yöntemler mevcuttu. Teşekkürler! – aioobe

    +0

    evet, pls. Grizzly 2.3.5+ kullan – alexey