实现http请求,使用RestTemplate请求参数使用body,请求头加入header信息调用实现


/**
* post请求(body参数)
* @param url 请求地址
* @param bodyMapParam bodyMapParam 参数
* @param queryParam url参数
* @param headers 请求头
* @return
*/
public String postBody(String url, Map<String,String> bodyMapParam, MultiValueMap<String,String> queryParam, Map<String,String> headers){
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
//拼接请求头
if(headers != null && !headers.isEmpty()){
for(Map.Entry<String,String> entry : headers.entrySet()){
header.add(entry.getKey(),entry.getValue());
}
}
HttpEntity<Map> request = new HttpEntity<Map>(bodyMapParam, header);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(queryParam);
ResponseEntity<Object> responseEntity = restTemplate.postForEntity(builder.toUriString(), request, Object.class);
return JsonUtil.parseJson(responseEntity);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM