当前位置:网站首页>如何使用Feign构造多参数的请求
如何使用Feign构造多参数的请求
2022-06-22 19:25:00 【菜鸟是大神】
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.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/get"}
由异常可知,尽管我们指定了GET方法,Feign依然会使用POST方法发送请求。于是导致了异常。正确写法如下
方法一[推荐]
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@GetMapping("/get")
public User get0(@SpringQueryMap User user);
}
方法二[推荐]
@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注解指定请求的参数是什么。
方法三[不推荐]
多参数的URL也可使用Map来构建。当目标URL参数非常多的时候,可使用这种方式简化Feign接口的编写。
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User get2(@RequestParam Map<String, Object> map);
}
在调用时,可使用类似以下的代码。
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);
}
注意:这种方式不建议使用。主要是因为可读性不好,而且如果参数为空的时候会有一些问题,例如map.put("username", null); 会导致microservice-provider-user 服务接收到的username是"" ,而不是null。
POST请求包含多个参数
下面来讨论如何使用Feign构造包含多个参数的POST请求。假设服务提供者的Controller是这样编写的:
@RestController
public class UserController {
@PostMapping("/post")
public User post(@RequestBody User user) {
...
}
}
我们要如何使用Feign去请求呢?答案非常简单,示例:
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/post", method = RequestMethod.POST)
public User post(@RequestBody User user);
}边栏推荐
- Introduction to async profiler
- The real king of cache
- 三维天地助力实验室夯实完整质量体系管理
- 怎样实现网页端im即时通讯中的@人功能
- CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务
- It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!
- 他98年的,我玩不过他...
- Possible security vulnerabilities in NFT
- MYSQL 几个常用命令使用
- Multi transactions in redis
猜你喜欢

Cloud computing in the metauniverse to enhance your digital experience

Comment le sac à dos complet considère - t - il la disposition?

Possible security vulnerabilities in NFT

极客星球 | 业务监控及告警系统体系化建设之路

Teach you how to create SSM project structure in idea

数字化转型的失败原因及成功之道

The road to systematic construction of geek planet business monitoring and alarm system

Introduction of Neural Network (BP) in Intelligent Computing

完全背包如何考虑排列问题

CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions
随机推荐
MySQL中如何计算同比和环比
Scheduling with Testing
AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
[proteus simulation] NE555 delay circuit
UnityEditor 编辑器脚本执行菜单
【Proteus仿真】三极管组成的H桥驱动直流电机+按键正反转控制
Web technology sharing | [Gaode map] to realize customized track playback
R 语言 wine 数据集可视化
Redis持久化的几种方式——深入解析RDB
Precautions for Apollo use
【Proteus仿真】8x8Led点阵数字循环显示
Understand the index of like in MySQL
[proteus simulation] 8x8LED dot matrix digital cyclic display
Shell编程基础(第七篇:分支语句-if)
讲真,Kotlin 协程的挂起没那么神秘(原理篇)
MYSQL 几个常用命令使用
关于放大器失真的原因你了解多少呢?
CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions
leetcode.11 --- 盛最多水的容器
智能计算之神经网络(BP)介绍