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

}

}

相关推荐
码客-ygluu8 小时前
X3 LUA 异步分布式微服务集群框架
c++·架构·lua
Decay__13 小时前
VMware 安装 SQL Server 双节点 AlwaysOn 可用性组教程 —— WSFC、AG 与侦听器完整部署(系列核心篇)
运维·数据库·sql·mysql·架构
wangruofeng14 小时前
从 GIF 到 AVIF:网页图片格式演进与 2026 年选型指南
前端·架构
Softmaxmap15 小时前
从零做一个中文小模型:600 亿 token 数据、mHC + Engram 结构、到强化学习的全流程(开源)
架构
你三大爷15 小时前
从 Hessian 迁移到 protostuff:深嵌套模型反序列化耗时与高并发类加载锁的根治实战
后端·架构
小南家的青蛙17 小时前
Spring Cloud 微服务熔断限流实战
微服务
一条Go20 小时前
接收方合并去重:同一个人为什么只该收到一次通知
后端·架构·github
吴建旭 智宅焕20 小时前
智能家居全国交付标准化架构的轻重分离设计:重勘测节点与轻执行流程的系统实现
架构·智能家居
flash俊杰21 小时前
v0.5-01 让桌面 AI 助手记得住:预算组装、递归摘要、长期记忆与评估基线
数据库·安全·架构
leo在掘金21 小时前
只挂了11小时,5000台开发机被塞进SSH公钥
后端·架构