Feign GET请求参数传递问题

复制代码
@GetMapping("/list")
ReturnMessage<EventMonitorResp> getAlarmList(AlarmReq alarmReq);

如果这种方式定义则会报下面错误:

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' is not supported at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping

实际client定义和controller都是用GET请求,大概原因,feign将该get方法转为了post,导致出错。

这个问题的主要的原因就是Feign默认使用的连接工具实现类,发现只要你有对应的body体对象,就会强制把GET请求转换成POST请求。

Feign源码在解析含有@FeignClient注解的接口的时候,在创建代理对象的时候,代理对象在去解析含有@RequestParam注解的参数的时候,会将该参数增强到url上,而不是作为body传递。

如果controller中用对象接收参数,即使使用了@RequestParam注解,参数还是传不过去;

如果controller中用具体参数接收的,使用@RequestParam注解是可以传过去的;

@FeignClient(name = "target-service")

public interface TargetServiceClient {

@GetMapping("/api/resource")

ResourceResponse getResource(@RequestParam("param1") String param1, @RequestParam("param2") Integer param2);

}

public interface TargetServiceController {

@GetMapping("/api/resource")

public ResourceResponse getResource(@RequestParam("param1") String param1, @RequestParam("param2") Integer param2) {

}

}

相关推荐
奔跑的蜗牛ing12 小时前
CentOS Nginx 安装与静态文件服务器配置指南
前端·面试·架构
doiito13 小时前
【Web安全,微服务安全】HashiCorp Vault 项目深度分析报告
安全·微服务
还有多久拿退休金13 小时前
让飞书知识库跟着 commit 自己长:一套自动化知识库的真实实现
前端·算法·架构
爸爸61913 小时前
06 项目目录结构与编码规范:三层架构与 DAO 模式实践
华为·架构·harmonyos·鸿蒙系统
XMan_Liu13 小时前
AI聊天机器人架构学习
人工智能·架构·机器人
AI 小老六14 小时前
Agent 评测系统架构:从指标分层到 GT/Judge 闭环的工程化落地
人工智能·ai·架构·系统架构·可用性测试
阿里云云原生14 小时前
不再手动同步 Prompt 与 Skill:Nacos AI Registry 打造 Agent 时代的“代码仓库”
微服务
小帽子_12314 小时前
大型储能液冷温控系统原理:散热架构、流道设计与温控精准控制逻辑
架构·系统架构
庄周de蝴蝶14 小时前
两次考试,总算压线通过系统分析师
后端·架构
以和为贵15 小时前
🔥前端也能搞懂流式输出:从 SSE 到打字机效果
前端·人工智能·架构