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

}

}

相关推荐
鸣弦artha1 天前
Flutter框架跨平台鸿蒙开发——Flutter Framework层架构概览
flutter·架构·harmonyos
Tony Bai1 天前
【分布式系统】05 时间的幻象 —— Lamport 与 Vector Clock 如何重建分布式因果?
分布式
hongyucai1 天前
智能体技术架构的深度解析:从模型层到应用层的金融实践
金融·架构
廋到被风吹走1 天前
【数据库】【MySQL】高可用架构深度解析:从主从复制到自动切换
数据库·mysql·架构
sld1681 天前
集团化企业S2B2B系统建设指南:技术架构、核心功能与落地实践
架构
梁bk1 天前
[spring cloud] Seata分布式事务管理
分布式·spring·spring cloud
Guheyunyi1 天前
电气安全管理系统:架构、技术与智能预警体系
大数据·人工智能·科技·安全·架构
Pocker_Spades_A1 天前
面向云原生架构的时序数据库选型:在国际主流 TSDB 谱系中理解 Apache IoTDB比
云原生·架构·时序数据库
nihui1231 天前
微服务解决方案 netflix与alibaba哪个好?
微服务·云原生·架构
重学一遍1 天前
深啃项目第四篇-kafka
分布式·kafka