泽西岛的GZip编码 [英] GZip encoding in Jersey
问题描述
我正在Jersey 2中编写RESTful Web服务.我想支持响应的Gzip编码.遵循此答案,我在ResourceConfig
类中启用了org.glassfish.jersey.server.filter.EncodingFilter
.
I'm writing a RESTful web service in Jersey 2. I want to support the Gzip encoding for the responses. Following this answer, I enabled the org.glassfish.jersey.server.filter.EncodingFilter
in my ResourceConfig
class.
public class MyWebService extends ResourceConfig {
public MyWebService() {
register(EncodingFilter.class);
register(GZipEncoder.class);
register(DeflateEncoder.class);
}
}
在资源类上,我返回一个javax.ws.rs.core.Response
对象.
On my resource class, I'm returning a javax.ws.rs.core.Response
object.
@GET
@Path("api/configs")
public Response listConfigs() throws Exception {
List<UserConfig> configs = configService.getAll();
return Response.ok().entity(configs).build();
}
现在,当我点击此api时,会收到响应,但响应标头不包含Content-Encoding
标头,而是包含Transfer-Encoding: chunked
.
Now when I hit this api, I get a response but the response headers do not contain a Content-Encoding
header, rather it contains Transfer-Encoding: chunked
.
请求:
> GET /api/configs HTTP/1.1
> Accept-Encoding: gzip
响应:
> HTTP/1.1 200
> Transfer-Encoding: chunked
* Received 14.8 KB chunk
* Received 504 B chunk
* Received 15.2 KB chunk
* Received 506 B chunk
* Received 15.1 KB chunk
* Received 514 B chunk
响应中没有Content-Encoding: gzip
标头,也没有任何Content-Length
标头.
There is no Content-Encoding: gzip
header in the response, nor there is any Content-Length
header.
我在Tomcat 9
上使用Jersey 2.27
.
我还有其他配置吗?如何获取这两个标头并以gzip压缩的形式获得响应,而不是接收分块的响应?
Is there any other configuration I'm missing? How do I get these two headers and get the response as gzip compressed rather than receiving chunked response?
编辑:我注意到当我发送大文件(> 1000 KB)时,我同时获得了Content-Encoding: gzip
和Transfer-Encoding: chunked
标头.
Edit: I have noticed that when I send large files ( > 1000 KB) I get both the Content-Encoding: gzip
and Transfer-Encoding: chunked
headers.
推荐答案
究竟使用Transfer-Encoding
还是Content-Length
完全由容器决定.这取决于允许的缓冲区大小.
Whether to use Transfer-Encoding
or Content-Length
is entirely the container's discretion. It depends on the allowed buffer size.
如果容器必须设置Content-Length
标头,则它必须事先知道压缩响应的长度,因此,容器必须在内存中缓冲整个响应.
If the container has to set the Content-Length
header, it has to know the length of the compressed response beforehand, therefore, the container has to buffer the entire response in the memory.
对于Jersey,内容长度缓冲区大小由ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER
定义.默认情况下,这是8192 bytes
.
In case of Jersey, the content length buffer size is defined by ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER
. By default, this is 8192 bytes
.
您可以从MyWebService
类中轻松地增加它:
You can easily increase this from your MyWebService
class:
public class MyWebService extends ResourceConfig {
public MyWebService() {
register(EncodingFilter.class);
register(GZipEncoder.class);
register(DeflateEncoder.class);
...
property(ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 32768);
}
}
希望这会有所帮助.
这篇关于泽西岛的GZip编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!