当前位置:网站首页>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);
}
边栏推荐
- LeetCode 77. combination
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
- VLAN experiment of switching technology
- Database learning summary 5
- Classic literature reading -- deformable Detr
- Memcached installation
- BGP 路由優選規則和通告原則
- Monitoring uplink of VRRP
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
- LeetCode 47. Full arrangement II
猜你喜欢
线性dp(拆分篇)
Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
日志(常用的日志框架)
官方零基础入门 Jetpack Compose 的中文课程来啦!
ROS create workspace
uni-app开发中遇到的问题(持续更新)
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
Let every developer use machine learning technology
注解和反射详解以及运用
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
随机推荐
LeetCode 40. 组合总和 II
Cookie plugin and localforce offline storage plugin
Scheme and implementation of automatic renewal of token expiration
ROS create workspace
Decryption skills of encrypted compressed files
注解和反射详解以及运用
Reading classic literature -- Suma++
深入学习JVM底层(三):垃圾回收器与内存分配策略
LeetCode 283. Move zero
Shenji Bailian 3.53-kruskal
LeetCode 47. Full arrangement II
Redis key value database [seckill]
Pbootcms collection and warehousing tutorial quick collection release
BGP报文详细解释
LeetCode 77. 组合
Use of Arduino wire Library
Ros2 --- lifecycle node summary
锐捷EBGP 配置案例
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
日志(常用的日志框架)