1. Problem sorting:
Exception: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
It is obvious that when feign finally executes the http request, this method is considered POST, but the feign client defines RequestMethod.GET or @GetMapping, which causes an error.
So why does feign think that this method is post?
Source code tracking:
1. Let’s look at the feignClient annotation as the entry:
2. According to the consistent style of spring cloud, we open the FeignAutoConfiguration class to see the configuration logic:
Look at the annotation in the red box of the picture above: This class is triggered when there is no ILoadBalancer. Our project has enabled Ribbon, so it must exist. Look at the comments again: Load balancing ribbon clients need to use FeignRibbonClientAutoConfiguration to configure this class. go ~
As shown in the above picture, look at the red box comments: In the order of importing from top to bottom: HttpClientFeignLoadBalancedConfiguration>OkHttpFeignLoadBalancedConfiguration>DefaultFeignLoadBalancedConfiguration, the corresponding underlying http tool: httppclient>okhttp>HttpURLConnection
According to the http protocol definition, @RequestBody+ RequestMethod.GET is supported. So it depends on the different implementations of the toolkit. Checking the source code, I found that okhttp and HttpURLConnection are not supported (reported an error), only httppclient supports it. (The default HttpURLConnection will report an error)
We know that only httpclient supports @RequestBody+RequestMethod.GET, so we must meet the conditions and go to HttpClientFeignLoadBalancedConfiguration. Check out the source code:
It can be seen that the ApacheHttpClient class exists under the classpath. Let's add in the pom:
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency>
Finally, the feign-httpclient-9.5.0.jar package was loaded, and it opened and found that there was ApacheHttpClient.class. Click in and see that it was actually a httppclient.
Therefore, the pom introduces feign-httpclient--》ApacheHttpClient.class--》Go to HttpClientFeignLoadBalancedConfiguration--》When requesting, HttpClient--》Support @RequestBody+ RequestMethod.GET
2. Solution:
Introduced in pom
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency>
After maven is updated, check whether the feign-httpclient-9.5.0.jar package exists in the project.
Summarize
The above is the solution to the spring cloud feign that the editor introduced to you. The solution to the error of spring cloud feign does not support @RequestBody+ RequestMethod.GET. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!