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

}

}

相关推荐
jinanwuhuaguo11 分钟前
生态融合与基座成型——OpenClaw v2026.4.24 的功能完备性跃迁与基础设施化拐点(第七篇)
人工智能·安全·架构·kotlin·openclaw
.柒宇.1 小时前
SpringCloud微服务入门教程
spring·spring cloud·微服务
keep intensify1 小时前
MIT 6.824 lab3B/C
分布式·后端·golang
丷丩1 小时前
GeoAI Universal Platform架构重构实践:解决插件系统循环依赖,落地SDK优先架构
人工智能·架构·geoai
java1234_小锋1 小时前
RabbitMQ中有哪几种交换机类型?
分布式·rabbitmq
代码漫谈1 小时前
探索RabbitMQ集群:如何实现消息的高可用性和负载均衡
分布式·消息队列·rabbitmq·负载均衡
用户99045017780091 小时前
中小商家必看:每天刷3小时闲鱼找货源?这个工具能省下2个人力成本ai-goofish-monitor
架构
清水白石0089 小时前
Python 编程实战全景:从基础语法到插件架构、异步性能与工程最佳实践
开发语言·python·架构
ting945200010 小时前
HunyuanOCR 全方位深度解析
人工智能·架构
heimeiyingwang12 小时前
【架构实战】CQRS架构模式实战
架构