Jersey 2/Grizzly 中的 GZIP 编码 [英] GZIP encoding in Jersey 2 / Grizzly
本文介绍了Jersey 2/Grizzly 中的 GZIP 编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法在我的 Jersey 服务中激活 gzip 编码.这是我尝试过的:
I can't activate gzip-encoding in my Jersey service. This is what I've tried:
Started out with the
jersey-quickstart-grizzly2
archetype from the Getting Started Guide.
添加 rc.register(org.glassfish.grizzly.http.GZipContentEncoding.class);
(也试过rc.register(org.glassfish.jersey.message.GZipEncoder.class);
)
从 mvn exec:java
用 curl --compressed -v -o - http://localhost:8080/myapp/myresource 测试
结果如下:
> 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!
也就是说,尽管请求中有Accept-Encoding: deflate, gzip
,但响应中没有Content-Encoding: gzip
.
That is, despite Accept-Encoding: deflate, gzip
in the request, there is no Content-Encoding: gzip
in the response.
我在这里错过了什么??
What am I missing here??
推荐答案
试试如下代码:
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();
这篇关于Jersey 2/Grizzly 中的 GZIP 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文