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 分钟前
13-云原生大数据架构介绍:大数据世界的“弹性城市”
大数据·云原生·架构
小码哥06825 分钟前
代驾系统微服务容器化部署与灰度发布流程
微服务·云原生·代驾系统·代驾·代驾服务·同城代驾
u0104058361 小时前
分布式淘客系统的配置中心设计:Nacos在多环境配置管理的应用
分布式
Crazy Struggle2 小时前
推荐 .NET 8.0 开源项目伪微服务框架
微服务·.net 8.0·微服务框架
迎仔2 小时前
01-Hadoop 核心三剑客通俗指南:从“单机搬砖”到“包工队”
大数据·hadoop·分布式
ALex_zry2 小时前
分布式缓存与微服务架构的集成
分布式·缓存·架构
贾修行2 小时前
企业级网络安全架构实战:从防火墙部署到远程办公全解析
web安全·架构·智能路由器
一只专注api接口开发的技术猿3 小时前
淘宝商品详情API的流量控制与熔断机制:保障系统稳定性的后端设计
大数据·数据结构·数据库·架构·node.js
ALex_zry3 小时前
分布式缓存安全最佳实践
分布式·安全·缓存
小马爱打代码3 小时前
熔断限流从入门到实战:打造高可用微服务架构
微服务·云原生·架构