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

WebFlux声明式客户端无法映射application/zip到ClientResponse求助

解决方案:WebFlux流式下载ZIP并上传到S3

核心问题定位

你遇到的Content type 'application/zip' not supported for bodyType=org.springframework.web.reactive.function.client.ClientResponse错误,是因为错误地尝试将二进制ZIP响应体直接反序列化为ClientResponse对象——ClientResponse是整个HTTP响应的封装(包含头、状态、体),不能作为body的目标类型来映射。

正确实现步骤

1. 调整声明式客户端方法

修改客户端接口,让方法直接返回Mono<ClientResponse>,这样就能拿到完整的HTTP响应(包括响应头和数据流):

@GetMapping(value = "/download/zip", produces = MediaType.APPLICATION_ZIP_VALUE)
Mono<ClientResponse> downloadZip();

2. 同时提取Content-Length和数据流

通过ClientResponse可以分别获取响应头中的Content-Length和二进制数据流,无需额外处理:

client.downloadZip()
    .flatMap(response -> {
        // 从响应头获取Content-Length(服务端已返回,直接提取)
        long contentLength = response.headers()
            .contentLength()
            .orElseThrow(() -> new IllegalStateException("服务端未返回Content-Length"));
        
        // 获取ZIP文件的流式数据
        Flux<DataBuffer> zipDataFlux = response.bodyToFlux(DataBuffer.class);
        
        // 执行S3上传逻辑
        return uploadToS3(zipDataFlux, contentLength);
    })
    .subscribe(
        result -> System.out.println("上传完成"),
        error -> System.err.println("上传失败:" + error.getMessage())
    );

3. 流式上传到S3

利用Spring Cloud AWS的Reactive S3客户端(S3ReactiveOperations),结合拿到的Content-Length和Flux<DataBuffer>完成流式上传,无需分片:

private Mono<PutObjectResponse> uploadToS3(Flux<DataBuffer> dataFlux, long contentLength) {
    PutObjectRequest request = PutObjectRequest.builder()
        .bucket("你的S3桶名")
        .key("备份文件.zip")
        .contentLength(contentLength)
        .contentType(MediaType.APPLICATION_ZIP_VALUE)
        .build();
    
    return s3ReactiveOperations.putObject(request, dataFlux);
}

额外说明

  • 如果服务端返回的Content-Length准确可靠,完全不需要分片上传,上述流式方案是最简单高效的选择。
  • 若后续遇到Content-Length缺失或不准确的场景,再考虑启用S3分片上传(Spring Cloud AWS也提供了对应的分片上传API支持)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 15:36:09