这些答案的其余部分已经过时和/或过于复杂,应该是简单的IMO(gzip到现在已经存在多长时间了?比Java更长的时间...)。来自文档:
在application.properties 1.3+中
# 🗜️🗜️🗜️
server.compression.enabled=true
# opt in to content types
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css
# not worth the CPU cycles at some point, probably
server.compression.min-response-size=10240
在application.properties 1.2.2-<1.3中
server.tomcat.compression=on
server.tomcat.compressableMimeTypes=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css
早于1.2.2:
@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");
}
}
另请注意,这仅在运行嵌入式tomcat时有效:
如果您打算部署到非嵌入式tomcat,则必须在server.xml中启用它http://tomcat.apache.org/tomcat-9.0-doc/config/http.html#Standard_Implementation
IRL生产说明:
为了避免所有这些情况,请考虑在Tomcat之前使用具有nginx和/或haproxy或类似功能的代理/负载平衡器设置,因为它比Java / Tomcat的线程模型更有效,更轻松地处理静态资产和gzip MUCH。
您不想把猫扔进浴缸里,因为它忙着压缩东西而不是处理请求(或者更有可能在运行AWS账单时旋转线程/吃CPU /堆等待数据库IO发生)为什么传统的Java / Tomcat可能不是一个好主意,这取决于您在做什么,但是我离题了...)
裁判:https :
//docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/howto.html#how-to-enable-http-response-compression
https://github.com/spring-projects/spring-boot/issues/2031