I. gizp 压缩配置
1. 配置
SpringBoot 默认是不开启 gzip 压缩的,需要我们手动开启,在配置文件中添加两行
server:
compression:
enabled: true
mime-types: application/json,application/xml,text/html,text/plain,text/css,application/x-javascript
注意下上面配置中的mime-types
,在 spring2.0+的版本中,默认值如下,所以一般我们不需要特意添加这个配置
// org.springframework.boot.web.server.Compression#mimeTypes
/**
* Comma-separated list of MIME types that should be compressed.
*/
private String[] mimeTypes = new String[] { "text/html", "text/xml", "text/plain",
"text/css", "text/javascript", "application/javascript", "application/json",
"application/xml" };
2. 测试
写一个测试的 demo
@RestController public class HelloRest { @GetMapping("bigReq") public String bigReqList() { List<String> result = new ArrayList<>(2048); for (int i = 0; i < 2048; i++) { result.add(UUID.randomUUID().toString()); } return JSON.toJSONString(result); } }
3. 说明
虽然加上了上面的配置,开启了 gzip 压缩,但是需要注意并不是说所有的接口都会使用 gzip 压缩,默认情况下,仅会压缩 2048 字节以上的内容
如果我们需要修改这个值,通过修改配置即可
server:
compression:
min-response-size: 1024
# 测试环境
server:
port: 8054
connection-timeout: 18000000
compression: #gizp 压缩配置
enabled: false #开启了 gzip 压缩
mime-types: application/json,application/xml,text/html,text/plain,text/css,application/x-javascript
min-response-size: 1024