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

}

}

相关推荐
耿雨飞6 小时前
第三章:LangChain Classic vs. 新版 LangChain —— 架构演进与迁移指南
人工智能·架构·langchain
亚历克斯神8 小时前
Elasticsearch 全文搜索实战:构建企业级搜索引擎
java·spring·微服务
亚历克斯神8 小时前
Spring Boot 与 Elasticsearch 8.0 集成
java·spring·微服务
乐维_lwops9 小时前
五层架构全景解析:Lerwee 运维智能体如何实现 “从感知到行动”(二)
运维·架构·运维智能体
TechMasterPlus9 小时前
LangGraph 实战指南:构建状态驱动的 LLM 应用架构
人工智能·架构
小夏子_riotous10 小时前
Docker学习路径——2、安装
linux·运维·分布式·学习·docker·容器·云计算
LT101579744410 小时前
2026 年自动化测试工具对比:架构与场景深度评测
测试工具·架构·自动化
努力搬砖的咸鱼13 小时前
Label 与 Selector:Kubernetes 资源选择的核心机制
微服务·云原生·容器·架构·kubernetes
CoovallyAIHub13 小时前
无人机拍叶片→AI找缺陷:CEA-DETR改进RT-DETR做风电叶片表面缺陷检测,mAP50达89.4%
算法·架构·github
CoovallyAIHub13 小时前
混合训练反而更差?VLM Agent在训练前协调跨数据集标注,文档布局检测F-score从0.860提升至0.883
算法·架构·github