![](/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.