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

}

}

相关推荐
buligbulig28 分钟前
Hadoop环境安装和集群创建
大数据·hadoop·分布式
沪上企服通40 分钟前
上海地区代账服务供给侧的五种架构形态:从 pipeline SLA 到领域深度的选型笔记
笔记·架构
某林2121 小时前
大模型边缘部署到底层硬件闭环
python·架构·机器人·硬件架构·ros2
带娃的IT创业者1 小时前
开源新纪元:深度解析小米 MiMo Code 的技术架构与实战应用
架构·开源·大模型·小米·代码生成·技术架构·mimo code
橙臣程1 小时前
集群、分布式与微服务介绍
分布式·微服务·架构
Niuguangshuo2 小时前
TTS 架构的六代演进(六):第 6 代 · 端侧轻量化
架构
Sirius Wu2 小时前
OpenClaw Observability 并发安全(Per-Request Hook)完整详解
服务器·网络·人工智能·安全·ai·架构·aigc
2601_954706492 小时前
云虚拟化移动端架构深度解析:API 自动化集群管控实战与商用选型方案
运维·架构·自动化
盛夏绽放2 小时前
Hermes Agent 全栈安装、配置与Web端架构深度解析(官方命令版·最终优化)
前端·架构·hermes
allnlei2 小时前
React + FastAPI 前后端分离架构下的 Nginx 配置实践
react.js·架构·fastapi