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

}

}

相关推荐
阡陌数智2 分钟前
Llama 4 MoE 架构深度拆解:交替稀疏专家设计与生产环境推理落地实战
架构·llama
方方洛1 小时前
ray教程-00-前言与导读
人工智能·分布式·机器学习
方方洛1 小时前
ray教程-01-认识Ray
人工智能·分布式·机器学习
波加曼大王1 小时前
# vLLM不要迷信PagedAttention神话,聊聊线上藏着的内部碎片陷阱
java·架构
xn71331 小时前
EmbeddingGemma 2 270M 实测:278 Chunk、32 个查询与 RRF 反例
人工智能·后端·架构
漠野9232 小时前
画布的保存按钮背后,站着一个编译器
架构
两万五千个小时2 小时前
DeepSeek Harness 从 0 开始:20 goal模式
人工智能·程序员·架构
漠野9232 小时前
让每个节点自己决定:要不要跑,还是循环跑
架构
dcacheor2 小时前
现代桌面播放器的交互困境与架构演进:从单窗流媒体走向多模态视讯工作台
架构
Mahut2 小时前
我在 Mac 上做了个本地剪辑器
javascript·架构·产品