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

}

}

相关推荐
超级小猪11 分钟前
006-spring cloud alibaba之gateway网关-过滤器Filter
微服务
巧克力味的桃子15 分钟前
Spark 课程核心知识点复习汇总
大数据·分布式·spark
wenzhangli722 分钟前
告别手撸架构图!AI+Ooder实现漂亮架构+动态交互+全栈可视化实战指南
人工智能·架构·交互
一只鱼丸yo1 小时前
Service Mesh:微服务治理的下一代方案
微服务·云原生·service_mesh
Java 码农1 小时前
RabbitMQ集群部署方案及配置指南05
分布式·rabbitmq
小马爱打代码2 小时前
ZooKeeper:五种经典应用场景
分布式·zookeeper·云原生
容智信息2 小时前
Hyper Agent:企业级Agentic架构怎么实现?
人工智能·信息可视化·自然语言处理·架构·自动驾驶·智慧城市
czlczl200209252 小时前
从 SSO 登录到跨系统资源访问:OAuth2 全链路交互详解
java·spring boot·后端·spring·架构
虫小宝3 小时前
导购app佣金模式微服务拆分:领域驱动设计在返利系统中的实践
微服务·云原生·架构