前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用Feign构造多参数的请求

如何使用Feign构造多参数的请求

作者头像
用户1516716
发布2018-04-02 16:37:45
3K0
发布2018-04-02 16:37:45
举报
文章被收录于专栏:A周立SpringCloudA周立SpringCloud

最近经常有人在Spring Cloud中国社区(http://springcloud.cn)QQ群(157525002)里问到该问题。索性整理一下。

本节我们来探讨如何使用Feign构造多参数的请求。笔者以GET以及POST方法的请求为例进行讲解,其他方法(例如DELETE、PUT等)的请求原理相通,大家可自行研究。

GET请求多参数的URL

假设我们请求的URL包含多个参数,例如http://microservice-provider-user/get?id=1&username=张三 ,要如何构造呢?

我们知道,Spring Cloud为Feign添加了Spring MVC的注解支持,那么我们不妨按照Spring MVC的写法尝试一下:

代码语言:javascript
复制
 @FeignClient("microservice-provider-user")public interface UserFeignClient {  @RequestMapping(value = "/get", method = RequestMethod.GET)  public User get0(User user);}

然而,这种写法并不正确,控制台会输出类似如下的异常。

代码语言:javascript
复制
 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"}

由异常可知,尽管我们指定了GET方法,Feign依然会使用POST方法发送请求。

正确写法如下:

(1) 方法一

代码语言:javascript
复制
 @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);}

这是最为直观的方式,URL有几个参数,Feign接口中的方法就有几个参数。使用@RequestParam注解指定请求的参数是什么。

(2) 方法二

多参数的URL也可使用Map来构建。当目标URL参数非常多的时候,可使用这种方式简化Feign接口的编写。

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

在调用时,可使用类似以下的代码。

代码语言:javascript
复制
 public User get(String username, String password) {  HashMap<String, Object> map = Maps.newHashMap();  map.put("id", "1");  map.put("username", "张三");  return this.userFeignClient.get2(map);}

POST请求包含多个参数

下面我们来讨论如何使用Feign构造包含多个参数的POST请求。假设服务提供者的Controller是这样编写的:

代码语言:javascript
复制
 @RestControllerpublic class UserController {  @PostMapping("/post")  public User post(@RequestBody User user) {    ...  }}

我们要如何使用Feign去请求呢?答案非常简单,示例:

代码语言:javascript
复制
 @FeignClient(name = "microservice-provider-user")public interface UserFeignClient {  @RequestMapping(value = "/post", method = RequestMethod.POST)  public User post(@RequestBody User user);}

TIPS

(1) 本节相关代码,详见本书配套代码中的microservice-provider-user-multiple-params项目和microservice-consumer-movie-feign-multiple-params项目。

(2) 除本节讲解的方式外,我们也可编写自己的编码器来构造多参数的请求,但这种方式编码成本较高,代码可重用性较低。故此,本书不再赘述。

拓展阅读

(1) 希望Feign能够支持参数请求使用POJO的Issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/1253(2) 建议使用Feign原生的注解的Issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/659(3) 建议增强Feign的功能:https://github.com/spring-cloud/spring-cloud-netflix/issues/1360(4) 建议支持可选的Request Body(目前Feign当POST一个null时,会报异常):https://github.com/spring-cloud/spring-cloud-netflix/issues/1047

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-01-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 A周立SpringCloud 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • GET请求多参数的URL
    • 正确写法如下:
    • POST请求包含多个参数
    • TIPS
    • 拓展阅读
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档