一、restTemplate.exchange
public String restTemplatelistqwe() {
MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("uuid", "861854049739673");
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.set("Authorization", "3b1a505d-7fc3-42f3-af19-3d303bc377a6");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
ResponseEntity<String> res =
restTemplate.exchange("http://localhost:8887/safetydevice/yuanZhang/ownSituation",
HttpMethod.POST, requestEntity, String.class);
JSONObject data = JSONObject.parseObject(res.getBody());
List<Map<String, Object>> data1 = (List<Map<String, Object>>) data.get("rows");
System.out.println(data1.get(0).get("elseNums"));
return null;
}
public String restTemplatelist() {
Map<String, String> map = new HashMap<>();
map.put("imei", "861854049739673");
restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
HttpHeaders headers = new HttpHeaders();
headers.set("api-key", "NDbJtnvOZILNxXmLtUYH=a981JQ=");
String imeiNum = "861854049739673";
ResponseEntity<String> res =
restTemplate.exchange("http://api.heclouds.com/devices/getbyimei?imei="+imeiNum+"",
HttpMethod.GET, new HttpEntity<>(null, headers), String.class);
All imeiMsg = JSONObject.parseObject(res.getBody(), All.class);
return imeiMsg.getData().getOnline();
}
二、restTemplate.postForEntity(getForEntity)
- json形式提交(APPLICATION_JSON)
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("userId", String.valueOf(userId));
headers.add("token", token);
List<Map<String, Object>> mapList = new ArrayList<>();
Map hashMap = new HashMap();
hashMap.put("","");
hashMap.put("","");
mapList.add(hashMap);
ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, new HttpEntity<>(JSON.toJSON(mapList).toString(), headers), JSONObject.class);
- 表单形式提交(MULTIPART_FORM_DATA)
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("userId", String.valueOf(userId));
headers.add("token", token);
MultiValueMap paraMap = new LinkedMultiValueMap();
paraMap.add("","");
paraMap.add("","");
ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, new HttpEntity<>(paraMap, headers), JSONObject.class);