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

}

}

相关推荐
博、、24 分钟前
酒吧点餐小程序开发:从场景痛点到系统落地的全流程拆解
人工智能·架构·需求分析
lhldsg25 分钟前
酒吧点餐小程序开发:从需求分析到技术落地实战
java·前端·小程序·架构·交友
阳明山水1 小时前
概念漂移分类与自适应策略解析
人工智能·深度学习·算法·机器学习·架构
zhougl9961 小时前
从ZooKeeper到微服务生态:分布式协调核心原理
分布式·微服务·zookeeper
Dawson Zhu1 小时前
Agent 持续进化:从学习信号到参数更新的工程化路径
人工智能·语言模型·架构·aigc·agi
Dawson Zhu2 小时前
多模态与实时交互:语音 Agent 与 Computer Use 的架构分析
人工智能·语言模型·架构·aigc·agi
FakeOccupational2 小时前
【p2p、分布式,区块链笔记 IPFS】网关+多地址+HTTP RPC API+kubo-rpc-client
分布式·区块链·p2p
bgy66662 小时前
Ceph 分布式存储完整实战指南:架构、部署与全场景运维
分布式·ceph·架构
程序员贺加贝2 小时前
报表大-IN-优化-一条-product_profile-超大-IN-SQL-背后的报表任务治理
java·spring boot·sql·性能优化·架构
mldong11 小时前
流程实例如何沿着箭头走到结束:Rust 工作流引擎的执行内核
架构·rust