SpringBoot使用RestTemplate的示例详解

作者:java技术媛 时间:2021-10-22 10:11:45 

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<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
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<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("id", "123");
paramMap.add("name", "张三");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers);
ResponseEntity<String> 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<User> httpEntity = new HttpEntity<User>(user, headers);
ResponseEntity<String> 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<JSONObject> httpEntity = new HttpEntity<JSONObject>(obj, headers);
ResponseEntity<String> 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<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
paramMap.add("id", "123");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers);
ResponseEntity<String> 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<String, Object> paramMap = new HashMap<String, Object>();
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<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("id", "123");
ResponseEntity<String> 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<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null, headers);
ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
System.out.println("result:" + response.getBody());

来源:https://blog.csdn.net/jcc4261/article/details/129762753

标签:SpringBoot,RestTemplate
0
投稿

猜你喜欢

  • Android RecyclerLineChart实现图表绘制教程

    2023-10-02 11:29:44
  • 使用Springboot根据配置文件动态注入接口实现类

    2022-11-18 06:56:45
  • C#中csv文件与DataTable互相导入处理实例解析

    2023-01-16 19:32:35
  • 六款值得推荐的android(安卓)开源框架简介

    2023-06-24 01:46:54
  • SharedPreference 初始化源码解析

    2023-11-13 07:00:43
  • Java日常练习题,每天进步一点点(53)

    2023-08-12 00:55:00
  • C#实现策略模式

    2022-02-09 17:19:01
  • C++ 双向循环链表类模版实例详解

    2022-10-29 18:04:49
  • Android基于ListView实现类似Market分页加载效果示例

    2021-10-01 10:44:32
  • 聊聊Spring——AOP详解(AOP概览)

    2023-11-01 04:44:18
  • Java详细分析梳理垃圾回收机制

    2023-10-30 04:02:33
  • C#如何访问共享文件夹或者磁盘

    2023-11-08 09:43:44
  • Android沉浸式状态栏设计的实例代码

    2023-11-14 13:03:14
  • 对Java中JSON解析器的一些见解

    2023-02-05 20:53:15
  • C#使用Object类实现栈的方法详解

    2021-08-03 17:36:16
  • ShardingSphere jdbc集成多数据源的实现步骤

    2023-11-25 07:54:56
  • 详解C# 托管资源和非托管资源

    2021-10-04 20:57:34
  • Java远程连接Linux服务器并执行命令及上传文件功能

    2023-01-28 14:03:31
  • Android中使用GridView进行应用程序UI布局的教程

    2022-03-19 15:18:06
  • springboot实用配置详细图文教程

    2023-12-07 00:36:43
  • asp之家 软件编程 m.aspxhome.com