当前位置:网站首页>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);
}
边栏推荐
- Introduce uview into uni app
- Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
- Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
- Scheme and implementation of automatic renewal of token expiration
- The official zero foundation introduction jetpack compose Chinese course is coming!
- BGP 路由优选规则和通告原则
- LeetCode 77. combination
- BGP 路由優選規則和通告原則
- Singleton mode compilation
- Lambda expressions and method references
猜你喜欢

浏览器原理思维导图

LeetCode 90. Subset II

Problems encountered in uni app development (continuous update)

借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力

LeetCode 78. 子集

Data playback partner rviz+plotjuggler

Deep learning classification network -- vggnet

Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!

Little bear sect manual query and ADC in-depth study

WLAN相关知识点总结
随机推荐
[C language] simple implementation of mine sweeping game
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
稀疏数组(非线性结构)
Stc8h8k series assembly and C51 actual combat - keys allow key counting (using falling edge interrupt control)
Little bear sect manual query and ADC in-depth study
Ros2 --- lifecycle node summary
Decryption skills of encrypted compressed files
经典文献阅读之--SuMa++
Use of Arduino wire Library
LeetCode 90. 子集 II
Reading classic literature -- Suma++
I/o impressions from readers | prize collection winners list
Detailed notes of ES6
Generic classes and parameterized classes of SystemVerilog
Scheme and implementation of automatic renewal of token expiration
Web page user step-by-step operation guide plug-in driver js
Problems encountered in uni app development (continuous update)
网络相关知识(硬件工程师)
LeetCode 27. 移除元素
Google play academy team PK competition, official start!