当前位置:网站首页>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);
}
边栏推荐
- 数据回放伴侣Rviz+plotjuggler
- LeetCode 47. 全排列 II
- LeetCode 83. Delete duplicate elements in the sorting linked list
- JWT tool class
- No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
- 神机百炼3.52-Prim
- Shenji Bailian 3.53-kruskal
- VRRP之监视上行链路
- ROS2----LifecycleNode生命周期节点总结
- AttributeError: ‘str‘ object has no attribute ‘decode‘
猜你喜欢
VLAN experiment of switching technology
递归(迷宫问题、8皇后问题)
Memcached installation
The official zero foundation introduction jetpack compose Chinese course is coming!
LeetCode 78. subset
Lucene Basics
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
IPv6 experiment and summary
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
Classic literature reading -- deformable Detr
随机推荐
锐捷EBGP 配置案例
日期时间API详解
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
Browser principle mind map
复杂 json数据 js前台解析 详细步骤《案例:一》
WLAN相关知识点总结
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
深入了解JUC并发(二)并发理论
社区说|Kotlin Flow 的原理与设计哲学
Memcached installation
[C language] simple implementation of mine sweeping game
Decryption skills of encrypted compressed files
Cglib代理-代码增强测试
LeetCode 90. 子集 II
Don't use the new WP collection. Don't use WordPress collection without update
External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
Deep learning classification network -- alexnet
Spark overview
Bgp Routing preference Rules and notice Principles
Current situation analysis of Devops and noops