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

}

}

相关推荐
happy_king_zi2 分钟前
RabbitMQ直接查看队列中消息的内容
分布式·rabbitmq
车江毅34 分钟前
亿级O2O(智能设备)系统架构笔记【原创】
java·分布式·技术战略规划
小二·1 小时前
从零到上线:Spring Boot 3 + Spring Cloud Alibaba + Vue 3 构建高可用 RBAC 微服务系统(超详细实战)
vue.js·spring boot·微服务
cherry52301 小时前
Java大厂面试真题:Spring Boot + 微服务 + 缓存架构三轮技术拷问实录
jvm·spring boot·mysql·微服务·java面试·分布式架构·redis缓存
快手技术1 小时前
从“拦路虎”到“修路工”:基于AhaEdit的广告素材修复
前端·算法·架构
三口吃掉你1 小时前
微服务之网关(Spring Cloud Gateway)
java·网关·微服务·gateway
回家路上绕了弯2 小时前
QPS 百万级分布式数据库:高并发订单号生成方案设计与落地
分布式·后端
三口吃掉你2 小时前
微服务之OpenFeign、hystrix熔断降级、loadbalancer负载均衡
hystrix·微服务·负载均衡·openfeign
坚果的博客2 小时前
技术解析:鸿蒙 PC 为什么采用 aarch64 架构?
华为·架构·harmonyos
熊文豪3 小时前
在 openEuler 上部署 Kafka 集群:深度性能评测与优化指南
分布式·kafka·openeuler