Gzip 压缩在我的项目中不起作用 与 Spring boot 1.5.10.RELEASE
我使用的是Spring Boot 1.5.10.RELEASE版本。Gzip压缩不起作用。
http://localhost:9000--> http://localhost:8080/api/..
Angularjs & rest api on different Port. Enabled CrossOrigin 接受来自 angularjs ui 的请求。
使用嵌入式 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'
}
});
其余 API 响应大小约 25 MB,因此我想压缩响应。
我在 application.properties 中添加了众所周知的属性来应用 gzip 压缩。弹簧引导 1.5.10 支持的属性
# 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
我观察到网络选项卡,但没有观察到内容编码:响应标头中的gzip。
请求
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
客户端/服务器端是否需要进行任何更改?
---[Edit-1] -------------在单个项目中尝试了gzip,但在我的项目中不起作用。
以下是从浏览器调用 rest api 时的响应标头
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
-----[编辑-2]-----Ziplet----
使用ziplet依赖性,我能够压缩响应,但我想使用弹簧引导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
当使用ziplet时 - 我可以看到压缩过滤器:压缩过滤器已初始化
当使用Spring boot gzip压缩时,我没有观察到任何我可以假设gzip压缩启用的地方。
任何属性来调试弹簧引导gzip压缩与嵌入的tomcat?like logging.level.org.eclipse.jetty.server.handler.gzip=TRACE
如何验证服务器.压缩.启用?
在我的项目中,客户端/服务器端需要进行任何更改吗?
提前致谢。