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

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.dtcms.com/a/305555.html

相关文章:

  • 用Python+MySQL实战解锁企业财务数据分析
  • Redis:缓存雪崩、穿透、击穿的技术解析和实战方案
  • 【开源】一款开源、跨平台的.NET WPF 通用权限开发框架 (ABP) ,功能全面、界面美观
  • mybatis中的极易出现错误用法
  • OpenBayes 一周速览丨Self Forcing 实现亚秒级延迟实时流视频生成;边缘AI新秀,LFM2-1.2B采用创新性架构超越传统模型
  • cgroups测试cpu bug
  • 离线录像文件视频AI分析解决方案
  • Camera相机人脸识别系列专题分析之十九:MTK ISP6S平台FDNode传递三方FFD到APP流程解析
  • MSPM0开发学习笔记:二维云台画图(2025电赛 附源代码及引脚配置)
  • RHCA学习概述
  • 【音视频】WebRTC-Web 音视频采集与播放
  • Reflect从入门到实战
  • Java面试宝典:MySQL中的系统库
  • vue npm install卡住没反应
  • Three.js 与 React:使用 react-three-fiber 构建声明式 3D 项目
  • 深度学习(鱼书)day06--神经网络的学习(后两节)
  • Apple基础(Xcode①-项目结构解析)
  • Java 笔记 default 使用场景
  • Python 程序设计讲义(44):组合数据类型——集合类型:创建集合
  • 从0到1学PHP(七):PHP 与 HTML 表单:实现数据交互
  • HTML第一次作业
  • html的onBlur
  • VUE -- 基础知识讲解(三)
  • 鹏哥C语言_82_指针_指针数组
  • 简单线性回归模型原理推导(最小二乘法)和案例解析
  • Linux C:位运算符
  • 【前端】span和div都设置了text-align,为什么对span不起作用
  • python基础语法1,python语法元素(简单易上手的python语法教学)(课后习题)
  • 操作系统- lecture3(进程的定义)
  • LVS (Linux Virtual Server) 解析