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

}

}

相关推荐
Qiuner21 小时前
Spring Boot AOP (六)架构落地与最佳实践
spring boot·后端·架构
Blossom.1181 天前
Prompt工程与思维链优化实战:从零构建动态Few-Shot与CoT推理引擎
人工智能·分布式·python·智能手机·django·prompt·边缘计算
怒放吧德德1 天前
软考架构师:考试心得分享
程序员·架构
aigcapi1 天前
[深度观察] RAG 架构重塑流量分发:2025 年 GEO 优化技术路径与头部服务商选型指南
大数据·人工智能·架构
Gavin在路上1 天前
智能体之深入解析 LLM 多智能体 (Multi-Agent) 四大主流架构(6)
架构
袋鼠云数栈1 天前
企业数据资产管理核心框架:L1-L5分层架构解析
大数据·人工智能·架构
短剑重铸之日1 天前
7天读懂MySQL|Day 4:锁与并发控制
数据库·mysql·架构
苏近之1 天前
Rust 中实现定时任务管理
后端·架构·rust
无限大61 天前
为什么"虚拟现实"和"增强现实"不同?——从虚拟到混合的视觉革命
架构