当前位置: 首页 > ai >正文

Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决

本文主要介绍在Spring Cloud Gateway Server Web MVC中报错“Unsupported transfer encoding: chunked”问题的原因,及解决方案。

org.apache.hc.core5.http.NotImplementedException: Unsupported Unsupported transfer encoding: chunked 错误通常是由于 Feign 依赖的 HTTP 客户端与服务端使用的 chunked 传输编码不兼容 导致的。具体原因和解决方案如下:

原因分析

基于Spring Cloud Gateway Server Web MVC的API网关问题控制台输出报错信息如下:

org.apache.hc.core5.http.NotImplementedException: Unsupported transfer encoding: chunkedat org.apache.hc.core5.http.impl.DefaultContentLengthStrategy.determineLength(DefaultContentLengthStrategy.java:90) ~[httpcore5-5.3.4.jar:5.3.4]at org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection.receiveResponseEntity(DefaultBHttpClientConnection.java:355) ~[httpcore5-5.3.4.jar:5.3.4]at org.apache.hc.core5.http.impl.io.HttpRequestExecutor.execute(HttpRequestExecutor.java:213) ~[httpcore5-5.3.4.jar:5.3.4]at org.apache.hc.client5.http.impl.classic.InternalExecRuntime.lambda$execute$0(InternalExecRuntime.java:236) ~[httpclient5-5.4.4.jar:5.4.4]at org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager$InternalConnectionEndpoint.execute(PoolingHttpClientConnectionManager.java:791) ~[httpclient5-5.4.4.jar:5.4.4]at org.apache.hc.client5.http.impl.classic.InternalExecRuntime.execute(InternalExecRuntime.java:233) ~[httpclient5-5.4.4.jar:5.4.4]

虽然错误是报在API网关,但实际是需要在微服务中去解决。问题原因如下:

HTTP 中的 chunked 传输编码用于动态分块传输数据(适用于数据大小不确定的场景)。该错误表明:

  • 服务端返回的响应使用了 Transfer-Encoding: chunked 编码。
  • Feign 客户端使用的 HTTP 客户端(如 Apache HttpClient 5.x)不支持或未正确配置 chunked 编码处理。

解决方案

REST接口类型尽量采用具体类型,避免 Feign 客户端调用过程中导致推导类型失真。

例如:

@FeignClient(name = "rednote-content-microservice",fallback = ContentServiceClientFallback.class // 指定降级实现类
)
public interface ContentServiceClient {@GetMapping("/note/user/{userId}")ResponseEntity<?> getNotesWithUser(@PathVariable Long userId,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "12") int size);
}

改为:

@FeignClient(name = "rednote-content-microservice",fallback = ContentServiceClientFallback.class // 指定降级实现类
)
public interface ContentServiceClient {@GetMapping("/note/user/{userId}")ResponseEntity<NotesWithUserDto> getNotesWithUser(@PathVariable Long userId,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "12") int size);
}
@GetMapping("/user/{userId}")
public ResponseEntity<?> getNotesWithUser(@PathVariable Long userId,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "12") int size) {ResponseEntity<UserDto> response = userServiceClient.findByUserId(userId);UserDto user = response.getBody();// 获取用户笔记列表(分页)Page<Note> notePage = noteService.getNotesByUser(userId, page - 1, size);// 转换为 DTOList<NoteExploreDto> noteExploreDtoList = notePage.map(note -> noteService.toExploreDto(note, user)).getContent();Map<String, Object> map = new HashMap<>();map.put("user", user);map.put("noteList", noteExploreDtoList);map.put("currentPage", page);map.put("totalPages", notePage.getTotalPages());return ResponseEntity.ok().body(map);
}

改为:

@GetMapping("/user/{userId}")
public ResponseEntity<?> getNotesWithUser(@PathVariable Long userId,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "12") int size) {ResponseEntity<UserDto> response = userServiceClient.findByUserId(userId);UserDto user = response.getBody();// 获取用户笔记列表(分页)Page<Note> notePage = noteService.getNotesByUser(userId, page - 1, size);// 转换为 DTOList<NoteExploreDto> noteExploreDtoList = notePage.map(note -> noteService.toExploreDto(note, user)).getContent();NotesWithUserDto dto = new NotesWithUserDto(user, noteExploreDtoList, page, notePage.getTotalPages());ResponseEntity<NotesWithUserDto> responseEntity = ResponseEntity.ok().body(dto);return responseEntity;
}

参考引用

  • 本文同步至:https://waylau.com/spring-cloud-gateway-server-webmvc-unsupported-transfer-encoding-chunked
  • 源码见《跟老卫学Spring Cloud开发》开源免费教程, https://github.com/waylau/spring-cloud-tutorial/
  • 《Spring Boot 企业级应用开发实战》(北京大学出版社)
  • 《Spring Cloud 微服务架构开发实战》(北京大学出版社)
  • 《Spring 5 开发大全》(北京大学出版社)
http://www.xdnf.cn/news/16557.html

相关文章:

  • Jupyter Notebook安装使用
  • WebRTC核心组件技术解析:架构、作用与协同机制
  • Java容器化实践:Docker+K8s部署Spring Boot应用全流程
  • LLM—— 基于 MCP 协议(Streamable HTTP 模式)的工具调用实践
  • 《设计模式之禅》笔记摘录 - 11.策略模式
  • 二叉树的学习
  • 【Java】批量生成Excel放入文件夹并打zip压缩包
  • 八种AI记忆术,重构智能体的“大脑”
  • RFID 系统行业前沿洞察:技术跃迁与生态重构
  • 线性代数常见的解题方法
  • aws(学习笔记第五十课) ECS集中练习(2)
  • 【MySQL 数据库】MySQL索引特性(二)页目录(B和B+树)(非)聚簇索引 索引操作
  • APM32芯得 EP.27 | 告别IDE,为APM32F411打造轻量级命令行开发工作流
  • 《Computational principles and challenges in single-cell data integration》
  • Vite 模块动态导入之Glob导入
  • 微算法科技MLGO突破性的监督量子分类器:纠缠辅助训练算法为量子机器学习开辟新天地
  • PCB学习笔记(一)
  • LeetCode 面试经典 150_数组/字符串_轮转数组(6_189_C++_中等)(额外数组;转置)
  • dify + mcp 实现图片 ocr 识别
  • 实例教学FPN原理与PANet,Pytorch逐行精讲实现
  • [leetcode] Z字型变换
  • dify离线插件打包步骤
  • 手撕设计模式——智能家居之外观模式
  • C++线程详解
  • C++11 std::function 详解:通用多态函数包装器
  • 从0开始学习R语言--Day62--RE插补
  • 【ssh】ubuntu服务器+本地windows主机,使用密钥对进行ssh链接
  • Linux常用基础命令
  • 反射核心:invoke与setAccessible方法详解
  • Git 从入门到精通