当前位置:网站首页>How to use feign to construct multi parameter requests
How to use feign to construct multi parameter requests
2022-06-22 20:57:00 【A rookie is a great God】
TIPS
This article is based on Spring Cloud Greenwich SR1, Theoretically, it supports Finchley And higher .
This section discusses how to use Feign Construct a multiparameter request . The author takes GET And POST Explain with request as an example , The other way ( for example DELETE、PUT etc. ) The principle of the request is the same , Readers can study for themselves .
GET Request multi parameter URL
Suppose you need to ask for URL Contains multiple parameters , for example http://microservice-provider-user/get?id=1&username= Zhang San , How to use Feign Structure ?
We know ,Spring Cloud by Feign Added Spring MVC Annotation support , Then we might as well follow Spring MVC Let's try it :
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User get0(User user);
}
However , This is not the right way to write , The console will output an exception similar to the following .
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"}
It can be seen from the abnormality , Even though we have designated GET Method ,Feign Still use POST Method to send a request . So it leads to the abnormality . The correct way to write it is as follows
Method 1 [ recommend ]
@FeignClient("microservice-provider-user")
public interface UserFeignClient {
@GetMapping("/get")
public User get0(@SpringQueryMap User user);
}
Method 2 [ recommend ]
@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);
}
This is the most intuitive way ,URL There are several parameters ,Feign The methods in the interface have several parameters . Use @RequestParam The annotation specifies what the requested parameters are .
Method 3 [ Not recommended ]
Multiparametric URL You can also use Map To build . When the target URL When there are many parameters , This can be used to simplify Feign Interface writing .
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User get2(@RequestParam Map<String, Object> map);
}
In the call , You can use code similar to .
public User get(String username, String password) {
HashMap<String, Object> map = Maps.newHashMap();
map.put("id", "1");
map.put("username", " Zhang San ");
return this.userFeignClient.get2(map);
}
Be careful : This method is not recommended . Mainly because of poor readability , And if the parameter is empty, there will be some problems , for example map.put("username", null); It can lead to microservice-provider-user Service received username yes "" , instead of null.
POST The request contains multiple parameters
Let's discuss how to use Feign Construct a with multiple parameters POST request . Suppose the service provider's Controller It is written in this way :
@RestController
public class UserController {
@PostMapping("/post")
public User post(@RequestBody User user) {
...
}
}
How do we use Feign To ask ? The answer is very simple , Example :
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "/post", method = RequestMethod.POST)
public User post(@RequestBody User user);
}边栏推荐
猜你喜欢

讲真,Kotlin 协程的挂起没那么神秘(原理篇)

智能計算之神經網絡(BP)介紹

【观察】软件行业创新进入“新周期”,如何在变局中开新局?

Container container runtime (2): which is better for you, yum installation or binary installation?

【已解决】--go_out: protoc-gen-go: Plugin failed with status code 1.

Résolu: peut - on avoir plus d'une colonne auto - incrémentale dans un tableau

MySQL advanced (II)
![[proteus simulation] 74LS138 decoder water lamp](/img/30/7dbdead9c18788cd946b5541e76443.png)
[proteus simulation] 74LS138 decoder water lamp

AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性

Easydss problem and solution summary
随机推荐
[proteus simulation] NE555 delay circuit
Xunrui CMS custom data interface PHP executable code
AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
R 语言 wine 数据集可视化
How to calculate yoy and mom in MySQL
软件测试——测试用例设计&测试分类详解
A Dynamic Near-Optimal Algorithm for Online Linear Programming
EasyClick更新图库
树莓派环境设置
Cross domain cors/options
Visualization of R language penguins dataset
Gradle Build Cache引发的Task缓存编译问题
The road to systematic construction of geek planet business monitoring and alarm system
Oracle system/用户被锁定的解决方法
Overview of common loss functions for in-depth learning: basic forms, principles and characteristics
MySQL高级(二)
Feign FAQ summary
Cryptography series: certificate format representation of PKI X.509
How to consider the arrangement of complete knapsack
慕课5、服务发现-Nacos