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

}

}

相关推荐
liweiweili1263 分钟前
CPU架构介绍
架构
周杰伦_Jay16 分钟前
【免费云平台部署指南】按场景选型+全维度对比(附直达地址)
网络·人工智能·架构·开源·云计算
seven_76782309819 分钟前
【前瞻创想】Kurator架构演进与云原生未来
分布式·云原生·架构·kurator·全链路
卡奥斯开源社区官方24 分钟前
深度解析:极智G-X100芯片如何用5nm Chiplet架构重构MR空间计算技术栈
重构·架构·mr
Ya-Jun35 分钟前
架构设计模式:MVVM架构应用
flutter·架构
熊出没39 分钟前
微服务架构介绍
微服务·云原生·架构
Xの哲學39 分钟前
Linux内核数据结构:设计哲学与实现机制
linux·服务器·算法·架构·边缘计算
Hernon41 分钟前
微服务架构设计 - 单体架构
微服务·云原生·架构·系统架构
@鱼香肉丝没有鱼1 小时前
大模型分布式微调 & Xtuner
分布式·大模型微调·xtuner·llamafactory
Wokoo71 小时前
C/S 架构与 B/S 架构:核心差异 + 选型指南
分布式·后端·中间件·架构