Spring cloud feign does not support @ RequestBody + RequestMethod. GET error solution. feign @ requestbody

Source: Internet
Author: User

Spring cloud feign does not support @ RequestBody + RequestMethod. GET error solution. feign @ requestbody

1. Problem sorting:

Exception:Org. springframework. web. HttpRequestMethodNotSupportedException: Request method 'post' not supported

Obviously, this method is regarded as POST when feign executes the http request, but RequestMethod. GET or @ GetMapping is defined in feign client. An error is reported due to a conflict.

So why does feign think this method is post?

Source code tracking:

1. We can see from the feignClient annotation as the entry:

2. According to the consistent style of spring cloud, we open the FeignAutoConfiguration class to view the configuration logic:

Look at the note in the red box: this class is triggered only when ILoadBalancer does not exist. Ribbon is enabled for our project, so it must exist. Let's look at the note again: to load the Server Load balancer ribbon clients, you must configure the FeignRibbonClientAutoConfiguration class. Go ~

For example, see the red box comment: According to the import order from top to bottom: HttpClientFeignLoadBalancedConfiguration> OkHttpFeignLoadBalancedConfiguration> DefaultFeignLoadBalancedConfiguration, corresponding to the underlying http tool:Httpclient> okhttp> HttpURLConnection

According to the http Protocol definition, @ RequestBody + RequestMethod is supported. GET, the specific implementation of the toolkit is different. Check the source code and find that okhttp and HttpURLConnection are not supported (an error is reported), and only httpclient is supported. (An error will be reported when HttpURLConnection is used by default)

We know that only httpclient supports @ RequestBody + RequestMethod. GET, so we must meet the conditions by using HttpClientFeignLoadBalancedConfiguration. Let's look at the source code:

It can be seen that the ApacheHttpClient class exists under the class path. We will add the following in pom:

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

Finally loaded the feign-httpclient-9.5.0.jar package, open to see inside an ApacheHttpClient. class, click to see is actually a httpclient.

SoIntroduce feign-httpclient -- in pom, And there is ApacheHttpClient. class -- in the class path. Go to HttpClientFeignLoadBalancedConfiguration -- when requesting HttpClient -- Support @ RequestBody + RequestMethod. GET

2. solution:

Introduce in pom

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

After maven updates, check whether the feign-httpclient-9.5.0.jar package exists in the project.

Summary

The above is a small series of spring cloud feign does not support @ RequestBody + RequestMethod. the solution to GET errors is helpful to everyone. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.