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) {

}

}

相关推荐
亲爱的非洲野猪21 小时前
Apache Cassandra完全指南:架构、原理与生产实践深度解析
架构·apache·database·cassandra
酷酷的鱼1 天前
跨平台技术选型方案(2026年App实战版)
react native·架构·鸿蒙系统
xiaobaishuoAI1 天前
分布式事务实战(Seata 版):解决分布式系统数据一致性问题(含代码教学)
大数据·人工智能·分布式·深度学习·wpf·geo
The Open Group1 天前
架构驱动未来:2026年数字化转型中的TOGAF®角色
架构
鸣弦artha1 天前
Flutter 框架跨平台鸿蒙开发——Flutter引擎层架构概览
flutter·架构·harmonyos
这儿有一堆花1 天前
CDN 工作原理:空间换取时间的网络架构
网络·架构·php
这儿有个昵称2 天前
互联网大厂Java面试场景:从Spring框架到微服务架构的提问解析
java·spring boot·微服务·kafka·grafana·prometheus·数据库优化
程序猿阿伟2 天前
《异步分布式训练提速关键:梯度压缩的收敛稳定性操控指南》
分布式
廋到被风吹走2 天前
【数据库】【MongoDB】全栈深度指南:文档模型到分布式集群
数据库·分布式·mongodb
Xの哲學2 天前
Linux Tasklet 深度剖析: 从设计思想到底层实现
linux·网络·算法·架构·边缘计算