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

}

}

相关推荐
400分19 小时前
LangChain 最新版 Zero/One/Few-Shot 提示词模板实战详解
架构
贵慜_Derek20 小时前
《从零实现 Agent 系统》连载 03|控制循环:感知—决策—行动—反思
人工智能·设计模式·架构
Duang20 小时前
我把 Claude、Codex、Copilot、Gemini 拼成了一个工作流,接力写代码
人工智能·程序员·架构
白露与泡影20 小时前
轻量级微服务发布系统:Traefik + Nomad + Consul
微服务·架构·consul
humcomm20 小时前
如何利用AI进行智能监控
人工智能·架构
踩着两条虫20 小时前
AI 低代码引擎可视化设计器交互机制实战
前端·vue.js·人工智能·低代码·架构
珠海西格电力20 小时前
零碳园区的能源成本优势具体体现在哪些方面
大数据·人工智能·算法·架构·能源
heimeiyingwang21 小时前
【架构实战】监控告警Prometheus+Grafana:让系统问题无处遁形
架构·grafana·prometheus
小短腿的代码世界21 小时前
QCefView架构深度解析:从Chromium嵌入到Qt信号槽集成的完整技术链路
qt·架构