当前位置:网站首页>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);
}
边栏推荐
- 500. Keyboard line
- Google play academy team PK competition, official start!
- Shenji Bailian 3.53-kruskal
- Zhuanzhuanben - LAN construction - Notes
- Talking about MySQL database
- Summary of WLAN related knowledge points
- Pbootcms collection and warehousing tutorial quick collection release
- 栈(线性结构)
- 最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
- 深入了解JUC并发(一)什么是JUC
猜你喜欢
随机推荐
Detailed explanation of BGP message
链表(线性结构)
社区说|Kotlin Flow 的原理与设计哲学
Detailed steps of JS foreground parsing of complex JSON data "case: I"
Ruijie ebgp configuration case
经典文献阅读之--SuMa++
步骤详解 | 助您轻松提交 Google Play 数据安全表单
LeetCode 83. 删除排序链表中的重复元素
BGP中的状态机
来自读者们的 I/O 观后感|有奖征集获奖名单
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
Shenji Bailian 3.53-kruskal
LeetCode 77. 组合
Summary of WLAN related knowledge points
How to use mitmproxy
Shenji Bailian 3.54-dichotomy of dyeing judgment
Current situation analysis of Devops and noops
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
Mock simulate the background return data with mockjs