实现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);
}
原文地址:https://www.cnblogs.com/lixiangang/p/15408050.html