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

}

}

相关推荐
qq_3829492210 小时前
推荐:《Spring Cloud Alibaba 微服务架构实战课》—— 从零到一构建企业级微服务系统
微服务·云原生·架构
JAVA社区12 小时前
Java高级全套教程(十一)—— Kubernetes 超详细企业级实战详解
java·运维·微服务·容器·面试·kubernetes
薛定猫AI14 小时前
Codex 与 Claude Code 安装配置完全指南
大数据·人工智能·架构
GISer_Jing15 小时前
Claude Code插件系统全解析
前端·人工智能·ai·架构
KaMeidebaby15 小时前
卡梅德生物技术快报|peg 修饰调控 MXene/WS2 异质结,氨气传感器制备与机理研究
大数据·前端·人工智能·架构·spark·新浪微博
龙佚15 小时前
抖动缓冲与播放控制:平滑播放的艺术
前端·架构
X54先生(人文科技)16 小时前
《元创力》纪实录·卷宗2.1刻舟求剑:一场关于“唯一解”的范式战争
人工智能·架构·开源·零知识证明
@insist12316 小时前
系统架构设计师-软件质量属性战术与架构评估方法全解
架构·系统架构·软考·系统架构设计师·软件水平考试
@insist12317 小时前
系统架构设计师-五大经典软件架构风格详解与软考真题应用指南
架构·系统架构·软考·系统架构设计师·软件水平考试