You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring WebFlux配置WebClient禁止自动解压响应数据

解决WebClient自动解压Gzip响应的问题

要让WebClient保留原始的Gzip压缩字节数据,关键是禁用它默认启用的自动解压机制,具体有两种配置方式:

方式一:通过Reactor HttpClient禁用压缩

在构建WebClient时,自定义ReactorClientHttpConnector,并配置底层HttpClient关闭自动压缩处理:

@Bean
public WebClient webClient() {
    HttpClient httpClient = HttpClient.create()
            .compress(false); // 关闭自动解压功能

    return WebClient.builder()
            .clientConnector(new ReactorClientHttpConnector(httpClient))
            .build();
}

方式二:通过ExchangeStrategies禁用内容压缩

修改WebClient的ExchangeStrategies,调整默认编解码器的压缩配置:

@Bean
public WebClient webClient() {
    ExchangeStrategies strategies = ExchangeStrategies.builder()
            .codecs(configurer -> {
                // 禁用默认的内容解压支持
                configurer.defaultCodecs().enableContentCompression(false);
            })
            .build();

    return WebClient.builder()
            .exchangeStrategies(strategies)
            .build();
}

配置完成后,WebClient就不会自动解压Gzip响应,你可以通过toEntity(byte[].class)获取原始的压缩字节数据,同时响应头中的content-encoding字段也会被完整保留下来,满足代理服务原样转发的需求。

内容的提问来源于stack exchange,提问作者Honey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 05:20:59