好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

feign GET请求不支持对象传参的坑及解决

GET请求不支持对象传参

问题

?

1

2

@GetMapping ( "/getByParam" )

String hello(Student student) throws Exception;

如上,feign调用报错500。

解决方法

增加@SpringQueryMap

?

1

2

@GetMapping ( "/getByParam" )

String hello( @SpringQueryMap Student student) throws Exception;

feign发get请求遇到的坑

问题

?

1

2

@RequestMapping (value= "/test" ,method = RquestMethod.GET)

Result getA(String id);

如上,调用该方法报错请求方式post错误,请用get。

原因分析

feign将该get方法转为了post,导致出错。

这个问题的主要的原因就是Feign默认使用的连接工具实现类,发现只要你有对应的body体对象,就会强制把GET请求转换成POST请求。

Feign源码在解析含有@FeignClient注解的接口的时候,在创建代理对象的时候,代理对象在去解析含有@RequestParam注解的参数的时候,会将该参数增强到url上,而不是作为body传递。

加上@RequestParam后问题解决

修改后代码如下。

?

1

2

@RequestMapping (value= "/test" ,method = RquestMethod.GET)

Result getA( @RequestParam String id);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_28433521/article/details/116598478

查看更多关于feign GET请求不支持对象传参的坑及解决的详细内容...

  阅读:14次