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

}

}

相关推荐
sunny_3 小时前
⚡️ vite-plugin-oxc:从 Babel 到 Oxc,我为 Vite 写了一个高性能编译插件
前端·webpack·架构
兆子龙7 小时前
模块联邦(Module Federation)详解:从概念到手把手 Demo
前端·架构
Bigger9 小时前
告别版本焦虑:如何为 Hugo 项目定制专属构建环境
前端·架构·go
狗哥哥13 小时前
微前端架构下的平台级公共组件资源体系设计
前端·架构
两万五千个小时13 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
Mintopia14 小时前
思想长期停在事物表面的深层原因:认知机制、环境结构与技术化治理
架构
兆子龙14 小时前
React Compiler 来了:少写 useMemo,照样稳
前端·架构
兆子龙1 天前
用 React + Remotion 做视频:入门与 AI 驱动生成
前端·架构
一枚前端小姐姐1 天前
低代码平台表单设计系统技术分析(实战二)
低代码·架构·前端框架
爱勇宝1 天前
2026年前端生存规划:只会写页面的人,正在被悄悄淘汰
前端·后端·架构