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 webServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer() {
            @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");