首页 > 技术文章 > RestTemplate的用法总结

pro-wall-box 2020-02-20 14:32 原文

1.post

(1)带header

  public static String postUrlWithHeader(RestTemplate restTemplate, String url, HeaderModel header, String json) throws Exception{
   HttpHeaders requestHeaders = new HttpHeaders();
         requestHeaders.setContentType("UTF-8");
         HttpEntity<String> requestEntity = new HttpEntity<String>(json, requestHeaders);
         
   ResponseEntity<String> res = restTemplate.postForEntity(url, requestEntity , String.class);
   String temp2 = null;
   if(res.getStatusCodeValue()== 200 && !StringUtils.isEmpty(res.getBody())){
    
    String xmlStr = res.getBody().toString();
     // 将xml转为json
     JSONObject xmlJSONObj = XML.toJSONObject(xmlStr);
     
     // 设置缩进
     String jsonPrettyPrintString = xmlJSONObj.toString(4);
    JSONObject temp = JSON.parseObject(jsonPrettyPrintString);
    //取key=ResultUtil的值
    temp2 = JsonProcess.returnValue(temp, "ResultUtil");
    
   }
   return temp2; 
  }

 

(2)

restTemplate相关连接:

https://www.cnblogs.com/javazhiyin/p/9851775.html

 

推荐阅读