實現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