![](/img/trans.png)
[英]How to specify external properties files in application.properties in Spring Boot?
[英]How to specify GZip properties in Spring boot 2 application
我有 spring boot 2 应用程序和 REST API 客户端。 有一个 API 下载大字节数组(大约 85MB),所以我愿意使用 GZip 编码压缩它。 向 application.properties 文件添加了以下属性
server.compression.enabled=true
server.compression.min-response-size=1024
server.compression.mime-types=application/octet-stream
默认压缩会减小文件大小,但会迅速增加处理时间。 我看到 GZip 编码中有 0-9 的压缩级别。
我如何在 application.properties 文件中设置压缩级别。
通过将嵌入式服务器更改为码头来解决我的问题。
先排除embedded tomcat,在pom.xml中添加jetty依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
然后添加Custom JettyServletWebServerFactory
如下
@Configuration
public class GZipConfig {
@Bean
public JettyServletWebServerFactory jettyServletWebServerFactory() {
JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
factory.addServerCustomizers((Server server) -> {
GzipHandler gzipHandler = new GzipHandler();
gzipHandler.setInflateBufferSize(1);
gzipHandler.setHandler(server.getHandler());
gzipHandler.setIncludedMethods("GET", "POST", "DELETE", "PUT");
gzipHandler.setCompressionLevel(Deflater.BEST_SPEED);
HandlerCollection handlerCollection = new HandlerCollection(gzipHandler);
server.setHandler(handlerCollection);
});
return factory;
}
}
在这里我们可以添加压缩级别。
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.