当前位置: 首页 > 知识库问答 >
问题:

Gzip压缩在Spring boot 1.5.10版本的项目中不起作用

卫弘义
2023-03-14

使用嵌入式tomcat服务器部署Spring Boot应用程序。未使用http2属性,即server.http2.enabled=true

Angualrjs调用rest API。以下是$HTTP服务

$http({
  method: method,
  url: url,
  params: params,
  data: body,
  headers: {
    Authorization: token,
    "Content-type": 'application/json'
  }
});

Rest api响应大小约为25 MB,所以我想压缩响应。

# Enable response compression
server.compression.enabled=true

# The comma-separated list of mime types that should be compressed
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json

# Compress the response only if the response size is at least 1KB
server.compression.min-response-size=1024

请求

Request URL: http://localhost:9081/employee
Request Method: GET
Status Code: 200 
Remote Address: [::1]:9081
Referrer Policy: no-referrer-when-downgrade

响应报头

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:7000
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Sun, 28 Jun 2020 18:15:17 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=6E7C07874D0329E18A0C07E5E303F005; Path=/; HttpOnly
Transfer-Encoding: chunked
Vary: Origin
X-Application-Context: application
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

请求标头

Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Bearer eyJhbGciOiJIUzINiJ9.eyJyb2xlIjiU0VDVE9SSEVBRCIsImxldmVsRG93biI6IkVENzA0MTI7TU04MzcyNDtKTDgzNTwO0RNNDAwNzE7Skc3MzA0NjtFQzM0NjEzO05OMTY5Nzk7QUs2MDYzNztTVDE4NTg4O0FTMjczNTE7Q0I4MTg3OTtWQTc4MTk5O0NNOTM3MDA7QVkyMzYzNztKUzcwMDY4O0NCMTc2NzE7TksyMTU2MDtMUzg4OTg0O0FQNTg3MDg7VFcyjk0NTtKSzI1Nzc3O01TNDk5MjE7SkI4OTcyOTtNSDAyMTI3O01CMTUwODk7SU0xMjgwODtNQzcxOTc2O1JSMjAzMDI7TFM1ODk4MiIsImxldmVsVXAiOm51bGwsImRlbGVnYXRlZCI6bnVsbCwic29lSWQiOiJTUjQ0MTg1I0.*************
Cache-Control: no-cache
Connection: keep-alive
Content-type: application/json
Host: localhost:9081
Origin: http://localhost:7000
Pragma: no-cache
Referer: http://localhost:7000/build/standalone.html
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Date: Sun, 28 Jun 2020 18:12:29 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding

使用ziplet依赖关系,我能够压缩响应,但我想使用spring boot gzip压缩。

响应头-使用Ziplet时

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:7000
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Date: Mon, 06 Jul 2020 18:31:07 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=8465D2E81A1A9CE146255B6C545FBE30; Path=/; HttpOnly
Transfer-Encoding: chunked
Vary: Accept-Encoding
Vary: Origin
X-Application-Context: application
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

当使用Spring boot gzip压缩时,没有观察到任何东西,我可以假设gzip压缩已启用。

在我的项目中需要在客户机/服务器端进行任何更改吗?

提前道谢。

共有1个答案

卫博学
2023-03-14

正如您的问题,您使用的是Spring Boot1.5.10.Release。

对于该版本的框架,TomcatEmbeddedServletContainerFactory类负责启动嵌入式Tomcat容器。

该类和版本的源代码可以在这里找到:

在这个类中,可以找到customizeCompression:

private void customizeCompression(Connector connector) {
  ProtocolHandler handler = connector.getProtocolHandler();
  if (handler instanceof AbstractHttp11Protocol) {
    AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
    Compression compression = getCompression();
    protocol.setCompression("on");
    protocol.setCompressionMinSize(compression.getMinResponseSize());
    configureCompressibleMimeTypes(protocol, compression);
    if (getCompression().getExcludedUserAgents() != null) {
      protocol.setNoCompressionUserAgents(
          StringUtils.arrayToCommaDelimitedString(
              getCompression().getExcludedUserAgents()));
    }
  }
}

尝试在此方法上设置断点,并调试应用程序以查看是否实际启用了压缩。

如果不是,很可能是您的项目中存在某种错误的配置

例如,要配置Undertow,而不是Tomcat,可以使用以下命令:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

您也可以尝试以编程方式配置Tomcat压缩;参见Matsev在这个stackoverflow问题中的回答:

使用GZIP压缩与Spring Boot/MVC/JavaConfig与RESTful

 类似资料:
  • 我想用Gzip压缩我的web应用程序,我使用下面的类 压缩滤波器 更新缓存过滤器工作正常,但仍然没有gzip压缩,下面是Chrome中的响应头。 我有什么办法能让这一切成功吗?我真的需要帮助,谢谢

  • 问题内容: 我想使用Gzip压缩来压缩java中的输入流。 假设我们有一个未压缩的输入流(1GB数据..)。因此,我需要从源压缩的输入流: 问题答案: DeflaterInputStream不是您想要的,因为它缺少gzip标头/预告片,并且使用略有不同的压缩方式。 如果从OutputStream(推)更改为InputStream(拉),则需要做不同的事情。 GzipOutputStream的作用是

  • 问题内容: 我正在使用php的功能来执行HTTP请求。为了节省带宽,我决定使用添加标题。 显然,输出一个gzip编码的字符串,所以我用来解码该编码的字符串,但是将作为参数传递的数据出错。 我知道还有另一个功能可以解压缩压缩后的数据,但是它不包含在我的PHP版本中(也许仅在SVN上可用)。 我知道cUrl可以即时解码gzip流(没有任何问题),但是有人建议我使用它而不是cUrl。 您是否知道以其他方

  • 我想在JavaScript中做解压缩图像。我已经用C#使用gzip压缩了图像。如何在JavaScript中解压缩gzipped数据? C#代码

  • Gzip 中间件 Gzip 中间件使用 gzip 压缩方案来对HTTP响应进行压缩。 使用 e.Use(middleware.Gzip()) 自定义配置 使用 e := echo.New() e.Use(middleware.GzipWithConfig(middleware.GzipConfig{ Level: 5, })) 配置 GzipConfig struct { // Skipp

  • 我尝试使用FFMPEG和这个库压缩视频:https://github.com/guardianproject/android-ffmpeg-java