RestTemplate的使用
GET请求
getForObject
第一种
String result = externalRestTemplate.getForObject("http://127.0.0.1:8081/restTemplate/get", String.class);
第二种
String result = externalRestTemplate.getForObject("http://127.0.0.1:8081/restTemplate/get/{a}/{b}", String.class, "1","2");
第三种
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
String result = externalRestTemplate.getForObject("http://127.0.0.1:8081/restTemplate/get/{a}/{b}", String.class, map);
getForEntity
第一种
ResponseEntity<String> forEntity = externalRestTemplate.getForEntity("http://127.0.0.1:8081/restTemplate/get", String.class);
String body = forEntity.getBody();
第二种
ResponseEntity<String> forEntity = externalRestTemplate.getForEntity("http://127.0.0.1:8081/restTemplate/get?a={a}&&b={b}", String.class, "1", "2");
String body = forEntity.getBody();
第三种
Map<String, Object> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
ResponseEntity<String> forEntity = externalRestTemplate.getForEntity("http://127.0.0.1:8081/restTemplate/get?a={a}&&b={b}", String.class, map);
String body = forEntity.getBody();
POST请求
postForObject
第一种 传对象,不带参数
Map<String, String> requset = new HashMap<>();
requset.put("a", "1");
requset.put("b", "2");
String s = externalRestTemplate.postForObject("http://127.0.0.1:8081/restTemplate/post", requset, String.class);
第二种 有参数
Map<String, String> requset = new HashMap<>();
requset.put("a", "1");
requset.put("b", "2");
String s = externalRestTemplate.postForObject("http://127.0.0.1:8081/restTemplate/post?a={a}&b={b}", null, String.class, requset);
postForEntity
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
requestBody.add("uid", signatureRequest.getUid());
requestBody.add("signature", signatureRequest.getSignature());
requestBody.add("timestamp", signatureRequest.getTimestamp());
requestBody.add("nonce", signatureRequest.getNonce());
requestBody.add("data", signatureRequest.getData());
//HttpEntity
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(requestBody, new HttpHeaders());
//post
return externalRestTemplate.postForEntity(url, requestEntity, String.class);
通过请求接口,exchange()
方法一
get:
Map<String, String> requset = new HashMap<>();
requset.put("a", "1");
requset.put("b", "2");
ResponseEntity<String> exchange = externalRestTemplate.exchange("http://127.0.0.1:8081/restTemplate/get?a={a}&b={b}", HttpMethod.GET, null, String.class, requset);
String body = exchange.getBody();
post:
Map<String, String> requset = new HashMap<>();
requset.put("a", "1");
requset.put("b", "2");
HttpEntity<Map> requestEntity = new HttpEntity<>(requset, null);
ResponseEntity<String> exchange = externalRestTemplate.exchange("http://127.0.0.1:8081/restTemplate/post", HttpMethod.POST, requestEntity, String.class);
String body = exchange.getBody();
方法二
get:
Map<String, String> requset = new HashMap<>();
requset.put("a", "1");
requset.put("b", "2");
RequestEntity requestEntity = new RequestEntity(HttpMethod.GET,new URI("http://127.0.0.1:8081/restTemplate/get"));
ResponseEntity<String> exchange = externalRestTemplate.exchange(requestEntity,String.class);
String body = exchange.getBody();
return body;
post:
Map<String, String> requset = new HashMap<>();
requset.put("a", "1");
requset.put("b", "2");
RequestEntity requestEntity = new RequestEntity<Map>(requset,HttpMethod.POST,new URI("http://127.0.0.1:8081/restTemplate/post"));
ResponseEntity<String> exchange = externalRestTemplate.exchange(requestEntity,String.class);
String body = exchange.getBody();
返回结果是一个List
String url = "http://127.0.0.1:8081/restTemplate/get" + "/getall";
ParameterizedTypeReference<List<Map>> parameterizedTypeReference = new ParameterizedTypeReference<List<Map>>() {
};
ResponseEntity<List<Map>> response = externalRestTemplate.exchange(url, HttpMethod.GET, null, parameterizedTypeReference);
List<Map> deviceList = response.getBody();