1.get 请求
RestTemplate restTemplate = new RestTemplate();
String url = "";
JSONObject result = restTemplate.getForObject(url, JSONObject.class);
2.post请求
public static JSONObject postObject(String url,Map<String, Object> params){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("Authorization", "Basic "+String.valueOf(Base64.encode((appKey+":"+masterSecret).getBytes())));//某些特殊的请求需要验证参数
JSONObject jsonObj = null;
HttpEntity<String> formEntity = null;
if(!EmptyUtil.isEmpty(params)){
jsonObj = JSONObject.fromObject(params);
formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
}
JSONObject result = restTemplate.postForObject(url, formEntity, JSONObject.class);
return result;
}