SpringBoot使用RestTemplate

作者: adm 分类: java 发布时间: 2023-12-08

RestTemplate 是由 Spring 提供的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。

POST请求
postForObject
1、使用LinkedMultiValueMap作为参数(Form表单提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
 
MultiValueMap paramMap = new LinkedMultiValueMap();
paramMap.add("id", "123");
paramMap.add("name", "张三");
String result = template.postForObject(url, paramMap, String.class);
System.out.println("result:" + result);

2、使用Object作为参数(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
 
User user = new User(123, "张三");
String result = template.postForObject(url, user, String.class);
System.out.println("result:" + result);

3、使用JSONObject作为参数(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
 
JSONObject obj = new JSONObject();
obj.put("id", "123");
obj.put("name", "张三");
String result = template.postForObject(url, obj, String.class);
System.out.println("result:" + result);

postForEntity
1、使用LinkedMultiValueMap作为参数(Form表单提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
 
HttpHeaders headers = new HttpHeaders();
headers.set("token", "asdf");
 
MultiValueMap paramMap = new LinkedMultiValueMap();
paramMap.add("id", "123");
paramMap.add("name", "张三");
 
HttpEntity> httpEntity = new HttpEntity>(paramMap, headers);
ResponseEntity response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

2、使用Object作为参数(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
 
HttpHeaders headers = new HttpHeaders();
 
User user = new User(123, "张三");
HttpEntity httpEntity = new HttpEntity(user, headers);
ResponseEntity response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

3、使用JSONObject为参数(JSON提交)

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/update";
 
HttpHeaders headers = new HttpHeaders();
 
JSONObject obj = new JSONObject();
obj.put("id", "123");
obj.put("name", "张三");
HttpEntity httpEntity = new HttpEntity(obj, headers);
ResponseEntity response = template.postForEntity(url, httpEntity, String.class);
System.out.println("result:" + response.getBody());

exchange

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/productDetail";
 
HttpHeaders headers = new HttpHeaders();
MultiValueMap paramMap = new LinkedMultiValueMap();
paramMap.add("id", "123");
 
HttpEntity> httpEntity = new HttpEntity>(paramMap, headers);
ResponseEntity response = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
System.out.println("result:" + response.getBody());

postForObject和postForEntity方法的区别主要在于可以在postForEntity方法中设置header的属性,当需要指定header的属性值的时候,使用postForEntity方法。

exchange方法和postForEntity类似,但是更灵活,exchange还可以调用get、put、delete请求。

GET请求
getForObject

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/detail?id={id}";
 
Map paramMap = new HashMap();
paramMap.put("id", "123");
 
String result = template.getForObject(url, String.class, paramMap);
System.out.println("result:" + result);

getForEntity

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/detail?id={id}";
 
Map paramMap = new HashMap();
paramMap.put("id", "123");
 
ResponseEntity response1 = template.getForEntity(url, String.class, paramMap);
System.out.println("result:" + response1.getBody());

exchange

RestTemplate template = new RestTemplate();
String url = "http://127.0.0.1:8800/product/productDetail";
 
HttpHeaders headers = new HttpHeaders();
headers.set("token", "asdf");
 
HttpEntity> httpEntity = new HttpEntity>(null, headers);
ResponseEntity response = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
System.out.println("result:" + response.getBody());

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!