当前位置:网站首页>How to use feign to construct multi parameter requests

How to use feign to construct multi parameter requests

2022-06-22 20:57:00 A rookie is a great God

TIPS

This article is based on Spring Cloud Greenwich SR1, Theoretically, it supports Finchley And higher .

This section discusses how to use Feign Construct a multiparameter request . The author takes GET And POST Explain with request as an example , The other way ( for example DELETE、PUT etc. ) The principle of the request is the same , Readers can study for themselves .

GET Request multi parameter URL

Suppose you need to ask for URL Contains multiple parameters , for example http://microservice-provider-user/get?id=1&username= Zhang San  , How to use Feign Structure ?

We know ,Spring Cloud by Feign Added Spring MVC Annotation support , Then we might as well follow Spring MVC Let's try it :

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public User get0(User user);
}

However , This is not the right way to write , 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"}

It can be seen from the abnormality , Even though we have designated GET Method ,Feign Still use POST Method to send a request . So it leads to the abnormality . The correct way to write it is as follows

Method 1 [ recommend ]

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
  @GetMapping("/get")
  public User get0(@SpringQueryMap User user);
}

Method 2 [ recommend ]

@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 ,URL There are several parameters ,Feign The methods in the interface have several parameters . Use @RequestParam The annotation specifies what the requested parameters are .

Method 3 [ Not recommended ]

Multiparametric URL You can also use Map To build . When the target URL When there are many parameters , This can be used to simplify Feign Interface writing .

@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public User get2(@RequestParam Map<String, Object> map);
}

In the call , You can use code similar to .

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);
}

Be careful : This method is not recommended . Mainly because of poor readability , And if the parameter is empty, there will be some problems , for example map.put("username", null);  It can lead to microservice-provider-user  Service received username yes "" , instead of null.

POST The request contains multiple parameters

Let's discuss how to use Feign Construct a with multiple parameters POST request . Suppose the service provider's Controller It is written in this way :

@RestController
public class UserController {
  @PostMapping("/post")
  public User post(@RequestBody User user) {
    ...
  }
}

How do we use Feign To ask ? 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);
}

原网站

版权声明
本文为[A rookie is a great God]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221924216085.html