springboot开启gzip压缩(springboot 1.3以上适用)

springboot开启gzip压缩(springboot 1.3以上适用)
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说springboot开启gzip压缩(springboot 1.3以上适用),希望能够帮助大家进步!!!

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://fzy15116089232.blog.csdn.net/article/details/111319937

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

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/18153

发表评论