侧边栏

springboot开启gzip压缩

 

springboot 2.x开启gzip压缩

1.application.yml配置

server: 
  compression: 
    enabled: true
    min-response-size: 1
    mime-types: application/json

2.代码设置(application.yml配置与代码设置二选一)

@Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory  servletContainer) {
                Compression compression = new Compression();
                compression.setEnabled(true);
                compression.setMimeTypes(new String[]{"application/json"});
                DataSize ds = DataSize.ofBytes(1);
                compression.setMinResponseSize(ds);
                servletContainer.setCompression(compression);
                //servletContainer.setPort(8088);
            }
        };
    }

3.检查Content-Type一定是application/json

否则需要设置

response.setHeader("Content-Type", "application/json");

4.检查gzip是否起效

参考:https://www.cnblogs.com/SmilingEye/p/11734528.html

比较设置前和设置后的文件大小

posted on 2019-10-25 15:22  SmilingEye  阅读(2802)  评论(0编辑  收藏  举报

导航