RestTemplate
RestTemplate是一个用于发送HTTP请求并获取响应的客户端工具
基础配置
配置 pom.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 项目模型版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 项目坐标 -->
<groupId>local.ateng.java</groupId>
<artifactId>rest-template</artifactId>
<version>v1.0</version>
<name>rest-template</name>
<description>RestTemplate是一个用于发送HTTP请求并获取响应的客户端工具</description>
<!-- 项目属性 -->
<properties>
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.4.1</spring-boot.version>
<maven-compiler.version>3.12.1</maven-compiler.version>
<lombok.version>1.18.36</lombok.version>
<fastjson2.version>2.0.53</fastjson2.version>
</properties>
<!-- 项目依赖 -->
<dependencies>
<!-- Spring Boot Web Starter: 包含用于构建Web应用程序的Spring Boot依赖项 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Test: 包含用于测试Spring Boot应用程序的依赖项 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Lombok: 简化Java代码编写的依赖项 -->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- 高性能的JSON库 -->
<!-- https://github.com/alibaba/fastjson2/wiki/fastjson2_intro_cn#0-fastjson-20%E4%BB%8B%E7%BB%8D -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<!-- Apache HttpComponents客户端 -->
<!--<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>-->
</dependencies>
<!-- Spring Boot 依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 插件仓库配置 -->
<repositories>
<!-- Central Repository -->
<repository>
<id>central</id>
<name>阿里云中央仓库</name>
<url>https://maven.aliyun.com/repository/central</url>
<!--<name>Maven官方中央仓库</name>
<url>https://repo.maven.apache.org/maven2/</url>-->
</repository>
</repositories>
<!-- 构建配置 -->
<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<!-- Maven 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<!-- 编译参数 -->
<compilerArgs>
<!-- 启用Java 8参数名称保留功能 -->
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<!-- Spring Boot Maven 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<!-- 第一个资源配置块 -->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<!-- 第二个资源配置块 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application*</include>
<include>bootstrap*.yml</include>
<include>common*</include>
<include>banner*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
编辑配置文件
java
package local.ateng.java.rest.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* RestTemplate配置文件
*
* @author 孔余
* @email 2385569970@qq.com
* @since 2025-02-02
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
使用RestTemplate
创建测试类
java
@SpringBootTest
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
class RestTemplateApplicationTests {
private final RestTemplate restTemplate;
}1
2
3
4
5
2
3
4
5
GET请求
getForObject方法,获取响应体,将其转换为第二个参数指定的类型
java
@Test
void getForObject() {
//getForObject方法,获取响应体,将其转换为第二个参数指定的类型
JSONObject jsonObject = restTemplate.getForObject("https://www.wanandroid.com/article/list/0/json", JSONObject.class);
String string = JSON.toJSONString(jsonObject, JSONWriter.Feature.PrettyFormat);
System.out.println(string);
}1
2
3
4
5
6
7
2
3
4
5
6
7
getForEntity方法,返回值为ResponseEntity类型
java
@Test
void getForEntity() {
//getForEntity方法,返回值为ResponseEntity类型
// ResponseEntity中包含了响应结果中的所有信息,比如头、状态、body
ResponseEntity<JSONObject> response = restTemplate.getForEntity("https://www.wanandroid.com/article/list/0/json", JSONObject.class);
// 获取状态对象
HttpStatusCode httpStatusCode = response.getStatusCode();
System.out.println("StatusCode=" + httpStatusCode);
// 获取状态码
int statusCodeValue = response.getStatusCodeValue();
System.out.println("StatusCodeValue=" + statusCodeValue);
// 获取headers
HttpHeaders httpHeaders = response.getHeaders();
System.out.println("Headers=" + httpHeaders);
// 获取body
JSONObject body = response.getBody();
System.out.println(body);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
GET请求参数
url中有动态参数
java
@Test
void getForObject2() {
//url中有动态参数
String url = "https://www.wanandroid.com/article/list/{id}/json";
HashMap<String, String> map = new HashMap<>();
map.put("id", "3");
JSONObject jsonObject = restTemplate.getForObject(url, JSONObject.class, map);
System.out.println(jsonObject);
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
查询参数
java
@Test
void getForObjectWithQueryParams() {
// url中有动态路径参数
String url = "https://www.wanandroid.com/article/list/{id}/json";
// 设置路径参数
HashMap<String, String> pathParams = new HashMap<>();
pathParams.put("id", "3");
// 使用UriComponentsBuilder添加查询参数
String finalUrl = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("page", 1)
.queryParam("limit", 10)
.encode() // 强制使用UTF-8编码
.toUriString();
System.out.println(finalUrl);
// 发起请求并获取响应体
JSONObject jsonObject = restTemplate.getForObject(finalUrl, JSONObject.class, pathParams);
System.out.println(jsonObject);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
请求头
java
@Test
void getForObject3() {
// get请求,带请求头的方式,使用exchange
String url = "https://www.wanandroid.com/article/list/{id}/json";
// 请求头
HttpHeaders headers = new HttpHeaders();
headers.add("header-1", "V1");
headers.add("header-2", "Spring");
headers.add("header-3", "SpringBoot");
// url的参数
HashMap<String, String> uriVariables = new HashMap<>();
uriVariables.put("id", "3");
// HttpEntity:HTTP实体,内部包含了请求头和请求体
HttpEntity requestEntity = new HttpEntity(
null,//body部分,get请求没有body,所以为null
headers //头
);
// 使用exchange发送请求
ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(
url, //url
HttpMethod.GET, //请求方式
requestEntity, //请求实体(头、body)
JSONObject.class,//返回的结果类型
uriVariables //url中的占位符对应的值
);
System.out.println(responseEntity.getStatusCodeValue());
System.out.println(responseEntity.getHeaders());
System.out.println(responseEntity.getBody());
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
表单请求
发送form-data表单post请求
java
@Test
void postForObject() {
// 发送form-data表单post请求
String url = "https://api.apiopen.top/api/login";
//①:表单信息,需要放在MultiValueMap中,MultiValueMap相当于Map<String,List<String>>
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
//调用add方法填充表单数据(表单名称:值)
body.add("account", "309324904@qq.com");
body.add("password", "123456");
//②:发送请求(url,请求体,返回值需要转换的类型)
JSONObject jsonObject = restTemplate.postForObject(url, body, JSONObject.class);
System.out.println(jsonObject);
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
POST请求
使用postForObject
java
@Test
public void postRequestUsingPostForObject() {
String url = "https://jsonplaceholder.typicode.com/posts";
// 创建请求体
String requestBody = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
// 使用 postForObject 发送 POST 请求并返回响应体
String response = restTemplate.postForObject(url, requestBody, String.class);
// 打印响应
System.out.println(response);
}1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
使用exchange
java
@Test
public void postRequestUsingExchange() {
String url = "https://jsonplaceholder.typicode.com/posts";
// 构建请求体
String requestBody = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
// 创建HttpHeaders并设置Content-Type
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 创建HttpEntity,将请求体和请求头封装在一起
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
// 使用 exchange 发送 POST 请求
ResponseEntity<String> response = restTemplate.exchange(
url,
HttpMethod.POST, // 请求方法为 POST
entity, // 请求体和头
String.class // 响应类型
);
// 打印响应
System.out.println(response.getBody());
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25