After putting together Feign in the last article, let’s summarize the following use of Feign calling multi-parameter methods.
The GET method:
Spring Cloud supports Spring Mvc annotations for Feign. Localhost :8083/test? Id =1&name=coco, so if we say (User entity class has these two attributes)
@FeignClient("people-center")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User test(User user);
}
Copy the code
This is actually an error, because even if we specify GET, Feign will still request as POST, so we need to change it to something like this
@FeignClient("people-center")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User test(@SpringQueryMap User user);
}
Copy the code
Or is it
@FeignClient("people-center")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User test(@RequestParam("id") Long id,@RequestParam("name") String name);
Copy the code
Or is it
@FeignClient("people-center")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User test(@RequestParam Map<String, Object> map);
}
Copy the code
The second option is recommended because it is more intuitive
A POST request
@FeignClient("people-center")
public interface UserFeignClient {
@RequestMapping(value = "/post", method = RequestMethod.POST)
public User test(@RequestBody User user);
}
Copy the code
The POST request is simple, just write it as above