SpringBoot自动解压Gzip请求

SpringBoot automatically decompression gzip requests

目前Spring官方内置自动GZIP的解压,仅支持Undertow容器和Jetty容器

Server端自动解压GZIP报文

在server端增加如下配置:

Undertow容器

    @Bean
    public UndertowServletWebServerFactory undertowServletWebServerFactory() {
        UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
        factory.addDeploymentInfoCustomizers((deploymentInfo) -> {
            deploymentInfo.addInitialHandlerChainWrapper(handler -> new RequestEncodingHandler(handler)
                    .addEncoding("gzip", GzipStreamSourceConduit.WRAPPER));
        });
        return factory;
    }

Jetty容器

@Bean
public JettyServletWebServerFactory jettyServletWebServerFactory() {
    JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
    factory.addServerCustomizers(server -> {
        GzipHandler gzipHandler = new GzipHandler();
        gzipHandler.setInflateBufferSize(1);
        gzipHandler.setHandler(server.getHandler());

        HandlerCollection handlerCollection = new HandlerCollection(gzipHandler);
        server.setHandler(handlerCollection);
    });
    return factory;
}

注:请求端增加 Header -> content-encoding=gzip

Client端发送GZIP请求

​​​​​​​https://blog.csdn.net/longsky886/article/details/124828008https://blog.csdn.net/longsky886/article/details/124828008

参考

https://www.baeldung.com/spring-resttemplate-compressing-requests

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中实现高效的GZIP压缩可以通过配置来实现。下面是一个示例: 1. 在`application.properties`或`application.yml`文件中添加以下配置: ```yaml server.compression.enabled=true server.compression.mime-types=application/json,application/xml,text/html,text/css,text/javascript,application/javascript ``` 2. 在启动类中添加`@EnableCompression`注解: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.context.annotation.Bean; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.filter.ShallowEtagHeaderFilter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.servlet.DispatcherType; import javax.servlet.ServletContext; import javax.servlet.ServletException; import java.util.EnumSet; @SpringBootApplication @EnableWebMvc public class BootDemoApplication implements WebMvcConfigurer, ServletContextInitializer { public static void main(String[] args) { SpringApplication.run(BootDemoApplication.class, args); } @Override public void onStartup(ServletContext servletContext) throws ServletException { EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD); FilterRegistrationBean<CharacterEncodingFilter> characterEncodingFilter = new FilterRegistrationBean<>(); characterEncodingFilter.setFilter(new CharacterEncodingFilter()); characterEncodingFilter.addInitParameter("encoding", "UTF-8"); characterEncodingFilter.addInitParameter("forceEncoding", "true"); characterEncodingFilter.setDispatcherTypes(dispatcherTypes); characterEncodingFilter.addUrlPatterns("/*"); FilterRegistrationBean<ShallowEtagHeaderFilter> shallowEtagHeaderFilter = new FilterRegistrationBean<>(); shallowEtagHeaderFilter.setFilter(new ShallowEtagHeaderFilter()); shallowEtagHeaderFilter.setDispatcherTypes(dispatcherTypes); shallowEtagHeaderFilter.addUrlPatterns("/*"); servletContext.addFilter("characterEncodingFilter", characterEncodingFilter.getFilter()).addMappingForUrlPatterns(dispatcherTypes, true, "/*"); servletContext.addFilter("shallowEtagHeaderFilter", shallowEtagHeaderFilter.getFilter()).addMappingForUrlPatterns(dispatcherTypes, true, "/*"); } @Bean public FilterRegistrationBean<GzipFilter> gzipFilter() { FilterRegistrationBean<GzipFilter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new GzipFilter()); registrationBean.addUrlPatterns("/*"); return registrationBean; } } ``` 请注意,上述示例中的`GzipFilter`是自定义的GZIP过滤器,你需要根据自己的需求实现该过滤器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值