一、Feign请求参数说明spring
电子商务平台源码请加企鹅求求:一零三八七七四六二六。Feign是Netflix的产品,Spring Cloud Feign是在原生Feign的基础上进行了封装。因为Spring Cloud Feign引入了许多springmvc的注解,方便springmvc的使用者使用,但同时也给使用者带来了很大的混淆,下面咱们简单讲解一下springmvc和Spring Cloud Feign的参数绑定机制。api
1.1 SpringMVC请求参数绑定机制mvc
咱们经过下面的例子进行讲解:app
@RestController public class demoController{ @RequestMapping("hello") public String hello(String name){ return "hello"+name; } }
虽然接口很简单,可是SpringMVC在解析请求时为作了不少的事情微服务
@RequestMapping指定请求的路径映射,咱们GET,POST,DELETE,PUT的请求均可以映射到这里;url
SpringMVC提供的参数注解包括@RequestParm,@RequestBody,@PathVariable等,在例子中咱们的参数name被默认添加@RequestParm注解,SpringMVC使用字节码技术获取name这个名称,自动检测请求参数中key值为name的参数,若是咱们的url请求或者form表单中包含name这个参数,就会被SpringMVC解析到。日志
1.2 Spring Cloud Feign请求参数绑定机制code
SpringMVC的参数绑定机制和Feign的参数绑定机制是不同的。下面咱们看一个错误的例子:orm
假若有这样一个api:对象
@ResstController public class demoController{ @RequestMapping(value="hello",method=RequsetMethod.GET) public String hello(String name){ return "hello"+name; }
Feign 访问这个api
@FeignClient(name = "hello") public interface IHelloService { @RequestMapping(value = "/hello",method = RequestMethod.GET) String hello (String name); }
因为咱们指定了请求方式GET,那么若是按照SpringMVC的参数绑定机制,name参数会被拼接在URL上,可是接口并未接收到,查看后台日志发现:
请求方式不是GET,而是POST
name 参数为null
通过Google发现Feign的参数绑定机制与SpringMVC不一样:
Feign默认使用@RequestBody,这就是上面name参数为null的缘由,@RequestBody(只能有一个)通常用于传递对象,若是参数中出现多个对象可使用Map来传递对象
@FeignClient(name = "hello") public interface IHelloService { @RequestMapping(value = "/hello",method = RequestMethod.GET) String saveBook(@RequestBody Book book); }
@FeignClient(name = "hello") public interface IHelloService { @RequestMapping(value = "/hello",method = RequestMethod.GET) String saveMap(@RequestParm Map map); }
Feign传递name参数,必须添加@RequestParm('name'),name必须指定,Feign不会利用SpringMVC字节码的机制自动给定一个默认名称
@FeignClient(name = "hello") public interface IHelloService { @RequestMapping(value = "/hello",method = RequestMethod.GET) String hello (@RequestParm('name') String name); }
二、@FeignClient注解与参数
@FeignClient注解被@Target(ElementType.TYPE)修饰,代表@FeignClient注解的做用在接口上
2.1 @FeignClient标签经常使用属性
name:指定FeignClient的名称,若是项目使用了Ribbon,name属性会做为微服务的名称,用于服务发现
decode404:当发生http 404错误时,若是该字段位true,会调用decoder进行解码,不然抛出FeignException
configuration: Feign配置类,能够自定义Feign的Encoder、Decoder、LogLevel、Contract
fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
fallbackFactory: 工厂类,用于生成fallback类示例,经过这个属性咱们能够实现每一个接口通用的容错逻辑,减小重复的代码
path: 定义当前FeignClient的统一前缀