您现在的位置是:首页 > 文章详情

java B2B2C Springcloud仿淘宝电子商城系统-声明式调用Feign之请求参数

日期:2019-04-17点击:325

1、Feign请求参数说明

Feign是Netflix的产品,Spring Cloud Feign是在原生Feign的基础上进行了封装。由于Spring Cloud Feign引入了许多springmvc的注解,方便springmvc的使用者使用,但同时也给使用者带来了很大的混淆,下面我们简单讲解一下springmvc和Spring Cloud Feign的参数绑定机制。

1.1 SpringMVC请求参数绑定机制

我们通过下面的例子进行讲解:

@RestController public class demoController{ @RequestMapping("hello") public String hello(String name){ return "hello"+name; } }

虽然接口很简单,但是SpringMVC在解析请求时为做了很多的事情

@RequestMapping指定请求的路径映射,我们GET,POST,DELETE,PUT的请求都可以映射到这里;

SpringMVC提供的参数注解包括@RequestParm,@RequestBody,@PathVariable等,在例子中我们的参数name被默认添加@RequestParm注解,SpringMVC使用字节码技术获取name这个名称,自动检测请求参数中key值为name的参数,如果我们的url请求或者form表单中包含name这个参数,就会被SpringMVC解析到。

1.2 Spring Cloud Feign请求参数绑定机制

SpringMVC的参数绑定机制和Feign的参数绑定机制是不一样的。下面我们看一个错误的例子:

假如有这样一个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); }

2、@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的统一前缀

java B2B2C Springcloud仿淘宝电子商城系统

原文链接:https://yq.aliyun.com/articles/698729
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章