In this section we will explore how to construct multi-parameter requests using Feign. The author uses the requests of GET and POST methods as examples to explain. The request principles of other methods (such as DELETE, PUT, etc.) are the same, and readers can study them themselves.
GET requests multiple parameters URL
Suppose the URL we request contains multiple parameters, such as http://microservice-provider-user/get?id=1&username=Zhang San, how to construct it?
We know that Spring Cloud has added Spring MVC annotation support to Feign, so we might as well try it according to the writing method of Spring MVC:
@FeignClient("microservice-provider-user")public interface UserFeignClient { @RequestMapping(value = "/get", method = RequestMethod.GET) public User get0(User user);}However, this writing method is not correct, and the console will output an exception similar to the following.
feign.FeignException: status 405 reading UserFeignClient#get0(User); content:
{"timestamp":1482676142940,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/get"}
From the exception, we can see that although we specify the GET method, Feign will still use the POST method to send the request.
The correct way to write it is as follows:
(1) Method 1
@FeignClient(name = "microservice-provider-user")public interface UserFeignClient { @RequestMapping(value = "/get", method = RequestMethod.GET) public User get1(@RequestParam("id") Long id, @RequestParam("username") String username);}This is the most intuitive way. The URL has several parameters, and the methods in the Feign interface have several parameters. Use the @RequestParam annotation to specify what the requested parameters are.
(2) Method 2
Multi-parameter URLs can also be built using Map. When there are many target URL parameters, this method can be used to simplify the writing of the Feign interface.
@FeignClient(name = "microservice-provider-user")public interface UserFeignClient { @RequestMapping(value = "/get", method = RequestMethod.GET) public User get2(@RequestParam Map<String, Object> map);}When calling, you can use code similar to the following.
public User get(String username, String password) { HashMap<String, Object> map = Maps.newHashMap(); map.put("id", "1"); map.put("username", "Zhang San"); return this.userFeignClient.get2(map);} POST request contains multiple parameters
Let's discuss how to construct a POST request containing multiple parameters using Feign. Suppose the service provider's controller is written like this:
@RestControllerpublic class UserController { @PostMapping("/post") public User post(@RequestBody User user) { ... }}How do we use Feign to request? The answer is very simple, example:
@FeignClient(name = "microservice-provider-user")public interface UserFeignClient { @RequestMapping(value = "/post", method = RequestMethod.POST) public User post(@RequestBody User user);} TIPS
(1) For details, please refer to the microservice-provider-user-multiple-params project and microservice-consumer-movie-feign-multiple-params project in the accompanying code of this book.
(2) In addition to the methods explained in this section, we can also write our own encoder to construct multi-parameter requests, but this method has a higher encoding cost and lower code reusability. Therefore, this book will not be repeated.
Extended reading
(1) I hope Feign can support parameter requests to use POJO's Issue: https://github.com/spring-cloud/spring-cloud-netflix/issues/1253
(2) Issue recommended to use Feign native annotations: https://github.com/spring-cloud/spring-cloud-netflix/issues/659
(3) It is recommended to enhance Feign's functionality: https://github.com/spring-cloud/spring-cloud-netflix/issues/1360
(4) It is recommended to support optional Request Body (currently, Feign will report an exception when POST a null): https://github.com/spring-cloud/spring-cloud-netflix/issues/1047
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.