Detailed explanation of spring cloud Feign problems encountered in the use of summary

  • 2021-01-11 01:59:16
  • OfStack

In this paper, the problems encountered in the use of spring cloud Feign are summarized and shared with you. The details are as follows:

Question 1:

We talked about that in the previous example


@RequestMapping(value = "/user/{id}", method = RequestMethod.GET) 
@GetMapping("/user/{id}") 

The effect of the two annotations is equivalent, but in the case of Feign, you can only use the above annotation, not the @GetMapping annotation. Let's change the @GetMapping annotation to the @GetMapping annotation in the previous example. We can see that the following exception is thrown when restarting the service:


Caused by: java.lang.IllegalStateException: Method findById not annotated with HTTP method type (ex. GET, POST) 

Exception means that we do not specify methods for HTTP

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 use it directly in the arguments of the findById method


@PathVariable Long id 

Let's change Feign to this


@RequestMapping(value = "/user/{id}", method = RequestMethod.GET) 
  User findById(@PathVariable Long id); 

Then we start the service, and we find that the exception is thrown again. The exception information is as follows:


Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0. 

The first parameter in the ES46en annotation cannot be null.


@RequestMapping(value = "/user/{id}", method = RequestMethod.GET) 
  User findById(@PathVariable("id") Long id); 

Restart and find 1 cut all ok.

Problem 3: Multi-parameter problem


@RequestMapping(value="/user/name", method=RequestMethod.GET) 
  User findByUsername(final String userName, final String address); 

When the service is started, the following exception is reported:

[

Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.chhliu.springboot.restful.vo.User com.chhliu.springboot.restful.feignclient.UserFeignClient.findByUsername(java.lang.String,java.lang.String)

]

Exception: when using Feign, if send get requests, you need to in the request parameters with @ RequestParam annotations, Controller inside can not add the annotations.

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

Examples of incorrect code for this:


@RequestMapping(value="/user/name", method=RequestMethod.GET) 
  User findByUsername(final String userName, @RequestParam("address") final String address); 

Note: the above userName @ RequestParam annotations modification parameters to use, and then send the request, will find that the called service 1 straight newspaper Request method 'POST not supported, we use GET method obviously, why is called service considered POST method, the reason is that when userName did not decorate be @ RequestParam annotations, will automatically be considered request body to deal with. Any body is considered an post request by feign, so the entire service is sent as an post request with request, parameter and body.


Related articles: