当前位置:网站首页>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 路由優選規則和通告原則
- 网络相关知识(硬件工程师)
- Detailed steps of JS foreground parsing of complex JSON data "case: I"
- BGP routing optimization rules and notification principles
- LeetCode 40. Combined sum II
- 注解和反射详解以及运用
- Pbootcms collection and warehousing tutorial quick collection release
- ROS2----LifecycleNode生命周期节点总结
- WLAN相关知识点总结
- Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
猜你喜欢

神机百炼3.54-染色法判定二分图

BGP报文详细解释

Memcached installation

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

ROS2----LifecycleNode生命周期节点总结

Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock

ROS create workspace

51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)

New version of dedecms collection and release plug-in tutorial tool

谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
随机推荐
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
BGP 路由优选规则和通告原则
LeetCode 77. combination
Go learning notes integration
sudo提权
Browser principle mind map
In depth understanding of JUC concurrency (II) concurrency theory
如何使用MITMPROXy
I/o multiplexing & event driven yyds dry inventory
Google Play Academy 组队 PK 赛,正式开赛!
日志(常用的日志框架)
Generic classes and parameterized classes of SystemVerilog
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
Frequently asked questions about jetpack compose and material you
BGP中的状态机
神机百炼3.54-染色法判定二分图
Current situation analysis of Devops and noops
Spark overview
步骤详解 | 助您轻松提交 Google Play 数据安全表单
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!