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

}

}

相关推荐
leoZ2311 分钟前
Claude 驱动的全栈开发:Spring Boot + Vue 的 BS 架构实践
vue.js·spring boot·架构
前端H1 小时前
微前端 v3 架构升级:从加载隔离到状态协同
前端·架构·状态模式
LYFlied1 小时前
Anthropic Agent隔离架构解读:比权限弹窗更可靠的是限制爆炸半径
人工智能·架构·软件工程·claude·安全架构·ai agent
Geek-Chow1 小时前
微服务认证与授权: 12 — JWKS 深入解析
spring·微服务·安全架构
visual_zhang2 小时前
文件行数如何影响 AI 编程的输出质量
架构
Larcher4 小时前
从回调地狱到优雅异步:Node.js 文件操作进化史
javascript·后端·架构
熊猫钓鱼>_>4 小时前
Electron:当 Web 技术统治桌面
大数据·前端·javascript·人工智能·架构·electron·agent
珠海西格电力7 小时前
零碳园区数据应用的具体场景有哪些?
大数据·人工智能·算法·架构·能源
东方小月7 小时前
agent-skills:把资深工程师的‘工作流’,装进你的AI编程里
前端·架构·代码规范
长不胖的路人甲8 小时前
微服务限流
微服务·云原生·架构