RestTemplate的用法总结

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)不带header

猜你喜欢

转载自www.cnblogs.com/pro-wall-box/p/12335602.html
今日推荐