温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring cloud feign中不支持@RequestBody+ RequestMethod.GET报错怎么办

发布时间:2021-07-12 11:59:52 来源:亿速云 阅读:219 作者:小新 栏目:编程语言

这篇文章主要介绍spring cloud feign中不支持@RequestBody+ RequestMethod.GET报错怎么办,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1.问题梳理:

异常:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

很明显是最终feign执行http请求时把这个方法认定为POST,但feign client中又定义了RequestMethod.GET 或 @GetMapping,冲突导致报错

那么为什么feign会认为这个方法是post呢?

源码追踪:

1.我们从feignClient注解作为入口来看:

spring cloud feign中不支持@RequestBody+ RequestMethod.GET报错怎么办

2.按照spring cloud一贯风格,我们打开FeignAutoConfiguration这个类看配置逻辑:

spring cloud feign中不支持@RequestBody+ RequestMethod.GET报错怎么办

看上图红框内的标注:这个类在不存在ILoadBalancer时才触发,我们项目开启了Ribbon,所以肯定存在,再看注释:载入负载均衡ribbon clients需要走FeignRibbonClientAutoConfiguration这个类配置。go~

spring cloud feign中不支持@RequestBody+ RequestMethod.GET报错怎么办

如上图,看红框注释:按照导入从上往下的顺序:HttpClientFeignLoadBalancedConfiguration>OkHttpFeignLoadBalancedConfiguration>DefaultFeignLoadBalancedConfiguration,对应的底层http工具:httpclient>okhttp>HttpURLConnection

根据http协议定义是支持@RequestBody+ RequestMethod.GET的,那么具体就得看工具包实现的不同的,查看源码发现okhttp和HttpURLConnection都不支持(报错),只有httpclient支持。(默认走HttpURLConnection会报错)

我们知道只有httpclient支持@RequestBody+ RequestMethod.GET,所以我们必须满足条件走HttpClientFeignLoadBalancedConfiguration才行,看下源码:

spring cloud feign中不支持@RequestBody+ RequestMethod.GET报错怎么办

可见,满足类路径下存在ApacheHttpClient类即可。我们再pom中添加:

<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-httpclient</artifactId>
 </dependency>

最终载入了feign-httpclient-9.5.0.jar包,打开看里面就一个ApacheHttpClient.class,点进去看其实就是一个httpclient。

spring cloud feign中不支持@RequestBody+ RequestMethod.GET报错怎么办

所以pom中引入feign-httpclient--》类路径下存在ApacheHttpClient.class--》走HttpClientFeignLoadBalancedConfiguration--》请求时走HttpClient--》支持@RequestBody+ RequestMethod.GET

2.解决方式:

 pom中引入

<dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-httpclient</artifactId>
 </dependency>

maven更新后查看项目中是否存在了feign-httpclient-9.5.0.jar包。

以上是“spring cloud feign中不支持@RequestBody+ RequestMethod.GET报错怎么办”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI