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

}

}

相关推荐
短剑重铸之日7 小时前
7天读懂MySQL|Day 5:执行引擎与SQL优化
java·数据库·sql·mysql·架构
喜欢吃豆9 小时前
OpenAI Realtime API 深度技术架构与实现指南——如何实现AI实时通话
人工智能·语言模型·架构·大模型
予枫的编程笔记10 小时前
Redis 核心数据结构深度解密:从基础命令到源码架构
java·数据结构·数据库·redis·缓存·架构
meilididiao12 小时前
低代码应用-动态指标跟踪评测系统
低代码·架构
Augustvic12 小时前
消息队列Kafka
分布式·kafka
秋42712 小时前
防火墙基本介绍与使用
linux·网络协议·安全·网络安全·架构·系统安全
java1234_小锋14 小时前
Zookeeper分布式锁如何实现?
分布式·zookeeper·云原生
程序员侠客行15 小时前
Mybatis二级缓存实现详解
java·数据库·后端·架构·mybatis
AutoMQ15 小时前
🎉 庆祝 AutoMQ 在 GitHub 上突破 9k Stars!
架构
Xの哲學16 小时前
Linux CFS 调度器深度解析
linux·服务器·算法·架构·边缘计算