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 小时前
ArkUI MVI 架构实战:单向数据流设计模式在 HarmonyOS NEXT 中的深度实践
华为·设计模式·架构·harmonyos
L-影4 小时前
单体与微服务区别
微服务·云原生·架构
cfm_29144 小时前
Nacos零基础入门实战
微服务
运维大师5 小时前
【K8S 运维实战】01-K8s架构深度剖析
运维·架构·kubernetes
liulilittle5 小时前
论无知:分布式
服务器·网络·分布式·并发·通信·竞态
XUHUOJUN5 小时前
Azure Stack Hub 容量规划指南(上篇:Microsoft 软件层 + 架构原理)
架构·azure stack
seacracker5 小时前
HPC存储I/O抖动无解?PowerFS基于CRDT弱一致架构,彻底实现前台零抖动
架构·分布式存储·powerfs·hpc存储·i/o零抖动
解局易否结局6 小时前
ArkTS 设计模式与架构进阶:构建可维护的 HarmonyOS 应用
华为·设计模式·架构·harmonyos
Swift社区7 小时前
AI 为什么越来越像微服务?
人工智能·微服务·架构
ai_coder_ai8 小时前
基于架构的软件设计方法
架构