当前位置:网站首页>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);
}边栏推荐
- Raspberry pie environment settings
- 跨域 CORS/OPTIONS
- Easyclick update Gallery
- R 语言 UniversalBank.csv“ 数据分析
- 阿里云视频点播播放出错,控制台访问出现code:4400
- Alibaba cloud video on demand playback error, console access code:4400
- ROS from entry to mastery (VIII) common sensors and message data
- One picture decoding opencloudos community open day
- CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions
- 用RNN & CNN进行情感分析 - PyTorch
猜你喜欢

R语言AirPassengers数据集可视化

MySQL高级(二)

what? You can't be separated by wechat

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

Alibaba cloud video on demand playback error, console access code:4400

一张图解码 OpenCloudOS 社区开放日

Raspberry pie environment settings
mysql8.0忘记密码的详细解决方法

AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
A detailed solution to mysql8.0 forgetting password
随机推荐
72-最近一次现场生产系统优化的成果与开发建议
阿里云视频点播播放出错,控制台访问出现code:4400
Lora technology -- Lora signal changes from data to Lora spread spectrum signal, and then from RF signal to data through demodulation
88-被广为流传的参数优化, 是蜜糖还是毒药?
启牛送的券商账户是安全的吗?启牛提供的券商账户是真的?
扩展Ribbon支持基于元数据的版本管理
智能计算之神经网络(Hopfield网络-DHNN,CHNN )介绍
Résolu: peut - on avoir plus d'une colonne auto - incrémentale dans un tableau
He was in '98. I can't play with him
Simple integration of client go gin 11 delete
Multi transactions in redis
Visualization of R language nutrient dataset
Easyclick update Gallery
Dynamicdatabasesource, which supports the master-slave database on the application side
How to consider the arrangement of complete knapsack
87-with as写法的5种用途
真正的缓存之王Caffine Cache
It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!
Stochastic Adaptive Dynamics of a Simple Market as a Non-Stationary Multi-Armed Bandit Problem
84-我对网传&lt;52 条 SQL 语句性能优化策略&gt;的一些看法