当前位置:网站首页>When requesting resttemplate, set the request header, request parameters, and request body.
When requesting resttemplate, set the request header, request parameters, and request body.
2022-07-02 06:19:00 【Amoeba】
public static <T> ResponseEntity<T> request(String url,
HttpMethod httpMethod,
Class<T> responseType,
Map<String,String> headers,
Map<String,String> params,
Map<String,String> body) {
// Set request parameters
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
if(CollUtil.isNotEmpty(params)){
params.forEach(paramsMap::add);
}
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
URI uri = builder.queryParams(paramsMap).build().encode().toUri();
// Set request header
HttpHeaders httpHeaders = new HttpHeaders();
// Must be set when passing the request body
httpHeaders.add("Content-Type", "application/json");
if( CollUtil.isNotEmpty(headers) ){
headers.forEach(httpHeaders::add);
}
// Set request header and request body
HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(body), httpHeaders);
return restTemplate.exchange(uri,httpMethod,httpEntity,responseType);
}
边栏推荐
- 程序员的自我修养—找工作反思篇
- In depth understanding of JUC concurrency (I) what is JUC
- Deep learning classification network -- Network in network
- 队列(线性结构)
- 稀疏数组(非线性结构)
- Google play academy team PK competition, official start!
- 利用传统方法(N-gram,HMM等)、神经网络方法(CNN,LSTM等)和预训练方法(Bert等)的中文分词任务实现
- 社区说|Kotlin Flow 的原理与设计哲学
- 加密压缩文件解密技巧
- Database learning summary 5
猜你喜欢
Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
State machine in BGP
WLAN相关知识点总结
Google play academy team PK competition, official start!
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
Problems encountered in uni app development (continuous update)
Classic literature reading -- deformable Detr
Shenji Bailian 3.53-kruskal
Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
Deep learning classification network -- Network in network
随机推荐
Detailed steps of JS foreground parsing of complex JSON data "case: I"
LeetCode 283. Move zero
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
BGP中的状态机
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
一起学习SQL中各种join以及它们的区别
Detailed explanation of BGP message
深入学习JVM底层(二):HotSpot虚拟机对象
LeetCode 39. 组合总和
【每日一题】—华为机试01
495. Timo attack
LeetCode 90. 子集 II
Eco express micro engine system has supported one click deployment to cloud hosting
ROS create workspace
Codeforces Round #797 (Div. 3) A—E
经典文献阅读之--Deformable DETR
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
500. Keyboard line
ZABBIX server trap command injection vulnerability (cve-2017-2824)