Spring Boot 版本:2.2.2.RELEASE
配置
修改 application.yml 配置文件,添加:
#-- 服务器
server:
compression:
enabled: true
mime-types:
- text/html
- text/xml
- text/plain
- text/css
- text/javascript
- application/javascript
- application/json
- application/xml
以上第 5 ~ 13 行,如果没有特殊要求,可以省略。
因为在 org.springframework.boot.web.server.Compression 类中,mimeTypes 已经有默认值(第 7、8 行):
package org.springframework.boot.web.server;
public class Compression {
private boolean enabled = false;
private String[] mimeTypes = new String[] { "text/html", "text/xml", "text/plain", "text/css", "text/javascript",
"application/javascript", "application/json", "application/xml" };
}
测试
未开启压缩功能之前,接收一个 JSON 传输的大小:
开启压缩功能之后,接收一个 JSON 传输的大小: