猿问

泽西岛中的 GZip 编码

我正在泽西岛2中编写RESTful网络服务。我想支持响应的Gzip编码。按照这个答案,我在课堂上启用了 。org.glassfish.jersey.server.filter.EncodingFilterResourceConfig


public class MyWebService extends ResourceConfig {

    public MyWebService() {

        register(EncodingFilter.class);

        register(GZipEncoder.class);

        register(DeflateEncoder.class);

    }

}

在我的资源类上,我返回一个对象。javax.ws.rs.core.Response


@GET

@Path("api/configs") 

public Response listConfigs() throws Exception {

    List<UserConfig> configs = configService.getAll();

    return Response.ok().entity(configs).build();

}

现在,当我点击此api时,我得到一个响应,但响应标头不包含标头,而是包含.Content-EncodingTransfer-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: gzipContent-Length


我正在使用 .Jersey 2.27Tomcat 9


我是否缺少任何其他配置?如何获取这两个标头,并以 gzip 压缩而不是接收分块响应的形式获取响应?


编辑:我注意到当我发送大文件(>1000 KB)时,我同时获得和标头。Content-Encoding: gzipTransfer-Encoding: chunked


侃侃尔雅
浏览 88回答 1
1回答

ibeautiful

是否使用或完全由容器自行决定。这取决于允许的缓冲区大小。Transfer-EncodingContent-Length如果容器必须设置标头,则必须事先知道压缩响应的长度,因此,容器必须在内存中缓冲整个响应。Content-Length对于泽西岛,内容长度缓冲区大小由 定义。缺省情况下,此值为 。ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER8192 bytes你可以从你的类中轻松增加这个:MyWebServicepublic class MyWebService extends ResourceConfig {&nbsp; &nbsp; public MyWebService() {&nbsp; &nbsp; &nbsp; &nbsp; register(EncodingFilter.class);&nbsp; &nbsp; &nbsp; &nbsp; register(GZipEncoder.class);&nbsp; &nbsp; &nbsp; &nbsp; register(DeflateEncoder.class);&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; property(ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 32768);&nbsp; &nbsp; }}希望这有帮助。
随时随地看视频慕课网APP

相关分类

Java
我要回答