TIPS
本文基于Spring Cloud Greenwich SR1,理论上支持Finchley及更高版本。
本节来探讨如何使用Feign构造多参数的请求。笔者以GET及POST请求为例讲解,其他方式(例如DELETE、PUT等)的请求原理相通,读者可自行研究。
GET请求多参数的URL
假设需请求的URL包含多个参数,例如http://microservice-provider-user/get?id=1&username=张三 ,该如何使用Feign构造呢?
我们知道,Spring Cloud为Feign添加了Spring MVC的注解支持,那么我们不妨按照Spring MVC的写法尝试一下:
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User get0(User user);
}
然而,这种写法并不正确,控制台会输出类似如下的异常。
feign.FeignException: status 405 reading UserFeignClient#get0(User); content:
{"timestamp":1482676142940,"status":405,"error":"Method Not Allowed","exception":"org.springframe