Summary of problems encountered in spring cloud Feign, cloudfeign
This article summarizes the problems encountered in spring cloud Feign and shares them with you as follows:
Question 1:
As mentioned in the previous example
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET) @GetMapping("/user/{id}")
The effects of these two annotations are equivalent, but in Feign usage, only the above method can be used, and @ GetMapping cannot be used directly. In the following example, change to the @ GetMapping annotation to check the effect. We found that the following exception was thrown when the service was restarted after the annotation was modified:
Copy codeThe Code is as follows:
Caused by: java. lang. IllegalStateException: Method findById not annotated with HTTP method type (ex. GET, POST)
The exception means that the HTTP method is not specified.
Question 2:
In the previous example, we exposed the Restful Service as follows:
@GetMapping("/template/{id}") public User findById(@PathVariable Long id) { return client.findById(id); }
Here, we directly use the findById Parameter
@PathVariable Long id
Next we will change the Feign method to this
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET) User findById(@PathVariable Long id);
Then we started the service and found that an exception was thrown again. The exception information is as follows:
Copy codeThe Code is as follows:
Caused by: java. lang. IllegalStateException: PathVariable annotation was empty on param 0.
It probably means that the first parameter of the PathVariable annotation cannot be blank. We will change it to the following method:
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET) User findById(@PathVariable("id") Long id);
Start again and find everything is OK.
Question 3: multiple parameters
@RequestMapping(value="/user/name", method=RequestMethod.GET) User findByUsername(final String userName, final String address);
When starting the service, the following exception is reported:
Caused by: java. lang. illegalStateException: Method has too encode Body parameters: public abstract com. chhliu. springboot. restful. vo. user com. chhliu. springboot. restful. feignclient. userFeignClient. findByUsername (java. lang. string, java. lang. string)
Cause of exception: when Feign is used, if a get request is sent, @ RequestParam annotation must be added before the request parameter. This annotation can be removed from the Controller.
The solution to the above problem is as follows:
@RequestMapping(value="/user/name", method=RequestMethod.GET) User findByUsername(@RequestParam("userName") final String userName, @RequestParam("address") final String address);
Question 4: Request method 'post' not supported
Sample error code:
@RequestMapping(value="/user/name", method=RequestMethod.GET) User findByUsername(final String userName, @RequestParam("address") final String address);
Note: The above userName parameter is not modified using the @ RequestParam annotation. Then, when a Request is sent, the called service always reports the Request method 'post' not supported. We use the GET method clearly, why is the POST method considered to be called by the service? The reason is that when the userName is not modified by the @ RequestParam annotation, it is automatically processed as the request body. As long as there is a body, it will be considered by feign as a post request, so the whole service is sent as a post request with request parameter and body.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.