码迷,mamicode.com
首页 > 其他好文 > 详细

RestTemplate的用法总结

时间:2020-02-20 14:50:37      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:utf-8   val   head   row   nbsp   XML   ISE   jsonp   obj   

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

 

RestTemplate的用法总结

标签:utf-8   val   head   row   nbsp   XML   ISE   jsonp   obj   

原文地址:https://www.cnblogs.com/pro-wall-box/p/12335602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!