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

}

}

相关推荐
伯奥克客服3 小时前
2026年视频综合一体机架构选型:从接口协议到流媒体底层的深度技术解析
架构·音视频
stark张宇6 小时前
分布式事务方案设计与选型思考:2PC、TCC
分布式·架构
zdr尽职尽责9 小时前
Unity 3C 动画系统:从「蜘蛛网」到可维护的动画架构
unity·架构·游戏引擎
Decay__9 小时前
SQLServer 监控体系构建 —— sql_exporter + Prometheus + Grafana 全栈实战
运维·数据库·sql·mysql·架构
装不满的克莱因瓶10 小时前
【AI时代必学】一篇文章教会你从零搭建Serverless架构
java·人工智能·ai·云原生·架构·serverless
一条大祥脚10 小时前
MemGPT 深度解析:让 Agent 拥有“无限上下文“的记忆架构
架构·agent·memgpt·智能体记忆
时间的拾荒人12 小时前
Redis 典型应用:缓存与分布式锁原理
redis·分布式·缓存·面试·架构
挖掘狂人13 小时前
飞书群聊已读,是怎么实现的?
java·程序员·架构
AAA修煤气灶刘哥13 小时前
为什么你的Codex 重连到 5/5 才开始工作?理解重试、协议回退与代理链路
人工智能·架构·openai
一条Go14 小时前
Kubernetes 多集群接入:kubeconfig 只进不出
后端·架构·github