当前位置:网站首页>RestTemplate请求时设置请求头,请求参数,请求体。
RestTemplate请求时设置请求头,请求参数,请求体。
2022-07-02 06:11:00 【爱码猿】
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) {
//设置请求参数
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();
// 设置请求头
HttpHeaders httpHeaders = new HttpHeaders();
// 传递请求体时必须设置
httpHeaders.add("Content-Type", "application/json");
if( CollUtil.isNotEmpty(headers) ){
headers.forEach(httpHeaders::add);
}
// 设置请求头和请求体
HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(body), httpHeaders);
return restTemplate.exchange(uri,httpMethod,httpEntity,responseType);
}
边栏推荐
- BGP routing optimization rules and notification principles
- Classic literature reading -- deformable Detr
- Zhuanzhuanben - LAN construction - Notes
- Introduce uview into uni app
- uni-app开发中遇到的问题(持续更新)
- Redis key value database [seckill]
- Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
- BGP 路由優選規則和通告原則
- AttributeError: ‘str‘ object has no attribute ‘decode‘
- Picture clipping plug-in cropper js
猜你喜欢

Eco express micro engine system has supported one click deployment to cloud hosting

Shenji Bailian 3.53-kruskal

LeetCode 90. 子集 II

State machine in BGP

Introduce uview into uni app

Lucene Basics

亚马逊aws数据湖工作之坑1

Sumo tutorial Hello World

Detailed explanation of BGP message

New version of dedecms collection and release plug-in tutorial tool
随机推荐
Detailed notes of ES6
如何使用MITMPROXy
LeetCode 283. 移动零
Stc8h8k series assembly and C51 actual combat - keys allow key counting (using falling edge interrupt control)
社区说|Kotlin Flow 的原理与设计哲学
递归(迷宫问题、8皇后问题)
AttributeError: ‘str‘ object has no attribute ‘decode‘
LeetCode 83. Delete duplicate elements in the sorting linked list
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
Monitoring uplink of VRRP
Detailed explanation of BGP message
Introduce uview into uni app
Step by step | help you easily submit Google play data security form
The real definition of open source software
uni-app开发中遇到的问题(持续更新)
ROS create workspace
复杂 json数据 js前台解析 详细步骤《案例:一》
Cookie plugin and localforce offline storage plugin
Pbootcms collection and warehousing tutorial quick collection release
Lucene Basics