Feign GET请求参数传递问题

文章讲述了Feign框架如何将GET请求自动转换为POST,当方法参数包含@RequestParam注解时。问题在于Feign的代理处理逻辑,解决办法是确保Controller接收具体参数而非对象,避免参数被添加到URL中。
摘要由CSDN通过智能技术生成
@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) {

                

        }
   } 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值