当前位置:网站首页>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);
}
边栏推荐
- Don't use the new WP collection. Don't use WordPress collection without update
- 稀疏数组(非线性结构)
- LeetCode 283. Move zero
- Sumo tutorial Hello World
- Lambda expressions and method references
- Bgp Routing preference Rules and notice Principles
- Data playback partner rviz+plotjuggler
- Jetpack Compose 与 Material You 常见问题解答
- Talking about MySQL database
- Detailed notes of ES6
猜你喜欢

递归(迷宫问题、8皇后问题)

Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability

深入学习JVM底层(三):垃圾回收器与内存分配策略

官方零基础入门 Jetpack Compose 的中文课程来啦!

IPv6 experiment and summary

State machine in BGP

Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed

社区说|Kotlin Flow 的原理与设计哲学

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

深入学习JVM底层(五):类加载机制
随机推荐
经典文献阅读之--Deformable DETR
Use some common functions of hbuilderx
Let every developer use machine learning technology
492. Construction rectangle
神机百炼3.54-染色法判定二分图
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
BGP 路由優選規則和通告原則
VLAN experiment of switching technology
Monitoring uplink of VRRP
链表(线性结构)
External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
State machine in BGP
Don't use the new WP collection. Don't use WordPress collection without update
New version of dedecms collection and release plug-in tutorial tool
Data playback partner rviz+plotjuggler
Google play academy team PK competition, official start!
Classic literature reading -- deformable Detr
Spark overview
Current situation analysis of Devops and noops
利用传统方法(N-gram,HMM等)、神经网络方法(CNN,LSTM等)和预训练方法(Bert等)的中文分词任务实现