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

}

}

相关推荐
ZhengEnCi43 分钟前
AI Agent 时代前端项目怎么组织:按页面纵向切块,替代按类型分层目录
架构·前端框架·前端工程化
知守观1 小时前
一个方法两个触发口:反编译 XXL-Job 后,我看清了 inner/job 子包的裸奔现状
java·spring boot·微服务
得物技术1 小时前
压测AI Agent 治理与评测调优实践
程序员·架构·测试
挖掘狂人3 小时前
负载均衡到底在干嘛?7 个场景给你说明白
架构·负载均衡
codigger3 小时前
负载均衡到底在干嘛?7 个场景给你说明白
网络·架构·负载均衡
Learn_PLC4 小时前
现代制造业转型下的自动化人才市场需求与职业教育法的影响
架构
一心同学5 小时前
Hermes架构拆解之Profiles
ai·架构·agent·profiles·hermes
IT规划师6 小时前
HybridCache-缓存架构决策框架落地
架构·.net
Eyfcom17 小时前
〖全球化架构 05〗跨国网络专线优化:QUIC 协议与 SD-WAN 在跨境传输中的实战
网络·架构·跨境物流快递系统·全球化架构