默认情况下,Web 应用程序不启用响应数据压缩。 需要对大文本类型的响应数据进行压缩,比如JSON、XML等应用数据,甚至JS、CSS等。
早期的Web应用程序基本上都要配置一个叫做这样的东西,然后判断请求的-是否包含gzip,然后压缩所需的-Type响应类型的数据。
使用后,当遇到压缩响应的需要时,首先要想到的是是否可以在. (或者.yml)。于是我查了2.7.x的帮助文档Boot,搜索关键字,翻了几页,找到了17.3.6。 HTTP,其中引入了三个配置项。
.=true(默认为false,不启用压缩).min--size=2048(默认情况下,至少2K字节及以上的响应数据会被压缩。必须在网络带宽和CPU消耗之间找到平衡).mim - types=text/html,text/xml,text/plain,text/css,text/,/json,/xml(默认压缩响应类型)
查找.A.11。 再往下,还有一个相关的选项
如果是1.2.x,启用压缩的方法是不同的。 具体可以参考官方文档64.18 HTTP
这里我们回到当前的2.7.x版本。 其实只要是1.3+版本,唯一要做的就是配置
server.compression.enable=true
其他三个选项基本满足我们的日常需求,或者可以根据需要稍作调整。
让我们进行一些实际的烟雾训练。 默认情况下没有配置它。 当 时,默认为 false,不启用响应压缩,并写入一个方法。
@GetMapping(value = "/hello")
public String hello(@RequestParam int length) {
return StringUtils.repeat("0", length);
}
@(值=“/你好”)
你好(@int){
。 (“0”,);
卷曲测试
bash-3.2$ curl -I http://localhost:8080/hello?length=2047 HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 2047 Date: Tue, 30 Aug 2022 03:01:53 GMT bash-3.2$ curl -I http://localhost:8080/hello?length=2048 HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 2048 Date: Tue, 30 Aug 2022 03:01:56 GMT bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2049 HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 2049 Date: Tue, 30 Aug 2022 03:02:20 GMT
无论如何,响应都不会被压缩,现在我们添加进去。
server.compression.enabled = true
。 。 = 真
重新测试
bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2047 HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 2047 Date: Tue, 30 Aug 2022 13:22:14 GMT bash-3.2$ curl -I http://localhost:8080/hello?length=2048 HTTP/1.1 200 vary: accept-encoding Content-Type: text/plain;charset=UTF-8 Content-Length: 2048 Date: Tue, 30 Aug 2022 13:22:16 GMT bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2048 HTTP/1.1 200 vary: accept-encoding Content-Encoding: gzip Content-Type: text/plain;charset=UTF-8 Transfer-Encoding: chunked Date: Tue, 30 Aug 2022 13:22:18 GMT
当响应长度为2048及以上时会使用压缩,并且无论是否有 -:gzip - 都会添加变化:以区分不同的响应数据。 例如,需要考虑——是否将压缩数据缓存为Key的一部分。
关于 .mime 类型
前面提到过,它的默认值是text/html、text/xml、text/plain、text/css、text/、/json、/xml,即只压缩这些-Type类型的数据,而不应该压缩的类型被压缩时注意不要重复压缩,如image/jpg、/octet-等。
text/plain 也适用于 -Type:text/plain;=UTF-8
不支持通配符配置。 例如text/*不能用于覆盖所有以text/开头的类型,如test/html、test/xml、text/plain等,必须一一列出
.mime-types中的配置区分大小写,如
server.compression.mime-types=TEXT/PLAIN
。 .mime-types=文本/纯文本
它对 -Type:text/plain 没有影响
bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2049 HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 2049 Date: Tue, 30 Aug 2022 14:20:21 GMT
如果我们将API的-Type设置为TEXT/PLAIN,它将被压缩
@GetMapping(value = "/hello")
public ResponseEntity hello(HttpServletResponse response, @RequestParam int length) {
MultiValueMap headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "TEXT/PLAIN");
return new ResponseEntity<>(StringUtils.repeat("0", length), headers, HttpStatus.OK);
}
@(值=“/你好”)
< > 你好(,@int){
< , > = 新 ( ) ;
。 添加(“-类型”,“文本/纯文本”);
新 ( . ( "0" , ) , , . 好的 ) ;
bash-3.2$ curl -I -H "Accept-Encoding:gzip" http://localhost:8080/hello?length=2049 HTTP/1.1 200 vary: accept-encoding Content-Encoding: gzip Content-Type: TEXT/PLAIN Transfer-Encoding: chunked Date: Tue, 30 Aug 2022 14:22:20 GMT
注意,在Web方法中,标准的-Type不能被@注解改变,
@GetMapping(value = "/hello", produces = "TEXT/PLAIN")
public String hello(HttpServletResponse response, @RequestParam int length) {
response.setHeader("Content-Type", "TEXT/PLAIN");
return StringUtils.repeat("0", length);
}
@(值=“/你好”,=“文本/普通”)
你好(,@int){
。 (“-类型”,“文本/纯文本”);
。 (“0”,);
上述代码最终的-Type仍然是text/plain;=UTF-8
其他相关内容
1.2.2 -
server.tomcat.compression=on
server.tomcat.compressableMimeTypes=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css
。 。 = 开
。 。 s = /json、/xml、文本/html、文本/xml、文本/纯文本、/、文本/css
1.2.2之前,作为嵌入式应用服务器使用时,通过mizer
@Component
public class TomcatCustomizer implements TomcatConnectorCustomizer {
@Override
public void customize(Connector connector) {
connector.setProperty("compression", "on");
// Add json and xml mime types, as they're not in the mimetype list by default
connector.setProperty("compressableMimeType", "text/html,text/xml,text/plain,application/json,application/xml");
}
}
@
类米泽尔{
@
空白 ( ) {
。 ( ““ , “在” ) ;
// 添加 json 和 xml mime 类型,因为它们不在列表中
。 ( "" , "text/html,text/xml,text/plain,/json,/xml" ) ;
可配置为自动实现 .xml 中响应数据的压缩。 您可以通过在第 10 章 - HTTP 中搜索来找到以下属性。