首页 归档 关于 文件 Github
×

RestTemplate 封装工具类

2021-07-02 10:54:00
SpringBoot
  • RestTemplate
本文总阅读量(次):
本文字数统计(字):1k
本文阅读时长(分):5

配置

ApplicationContextHelper

RestTemplate配置

说明

简单的 Get、Post 请求

自定义请求头的 Get、Post 请求

带参数的 Get(Query) 请求

带参数的 Post(Query/Body) 请求

可上传单个、多个文件的 Post 请求

代码

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* author :demon
* date :2021 2021/6/29 18:20
* desc :RestTemplate 工具
*/
public class RestTemplateUtil {

/**
* 自定义请求头
* 简单的 Get请求
*
* @param headers 参数
* @param url 参数
* @return 返回
*/
public static ResponseEntity<String> simpleGet(HttpHeaders headers, String url) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
//将请求头部和参数合成一个请求
HttpEntity<?> httpEntity = new HttpEntity<>(headers);
return restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, httpEntity, String.class);
}

/**
* 表单 - Query
* 带参数 Get请求
*
* @param headers 参数
* @param url 参数
* @param params 参数
* @return 返回
*/
public static ResponseEntity<String> executeGetParam(HttpHeaders headers, String url, Map<String, String> params) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
headers.set("Content-Type", "application/x-www-form-urlencoded");
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
if (params != null && params.size() > 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.queryParam(entry.getKey(), entry.getValue());
}
}
//将请求头部和参数合成一个请求
HttpEntity<?> httpEntity = new HttpEntity<>(headers);
return restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, httpEntity, String.class);
}


/**
* 自定义请求头
* - Query
* - Body
* 简单的 Post 请求
*
* @param headers 参数
* @param url 参数
* @return 返回
*/
public static ResponseEntity<String> simplePost(HttpHeaders headers, String url) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
//将请求头部和参数合成一个请求
HttpEntity<String> httpEntity = new HttpEntity<>(null, headers);
return restTemplate.postForEntity(url, httpEntity, String.class);
}


/**
* 表单 - Query
* 带参数的 Post 请求
*
* @param headers 参数
* @param url 参数
* @param params 参数
* @return 返回
*/
public static ResponseEntity<String> executePostFromParam(HttpHeaders headers, String url, Map<String, String> params) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
return restTemplate.postForEntity(url, getMultiValueMap(headers, null, params), String.class);
}

/**
* 表单 - Query
* 上传单个文件的 Post 请求
*
* @param headers 参数
* @param url 参数
* @param file 参数
* @return 返回
*/
public static ResponseEntity<String> executePostFromFile(HttpHeaders headers, String url, File file) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
List<File> files = new ArrayList<>();
files.add(file);
return restTemplate.postForEntity(url, getMultiValueMap(headers, files, null), String.class);
}

/**
* 表单 - Query
* 上传多个文件的 Post 请求
*
* @param headers 参数
* @param url 参数
* @param files 参数
* @return 返回
*/
public static ResponseEntity<String> executePostFromFile(HttpHeaders headers, String url, List<File> files) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
return restTemplate.postForEntity(url, getMultiValueMap(headers, files, null), String.class);
}

/**
* 表单 - Query
* 上传单个文件且带参数的 Post 请求
*
* @param headers 参数
* @param url 参数
* @param file 参数
* @param params 参数
* @return 返回
*/
public static ResponseEntity<String> executePostFromFileParam(HttpHeaders headers, String url, File file, Map<String, String> params) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
List<File> files = new ArrayList<>();
files.add(file);
return restTemplate.postForEntity(url, getMultiValueMap(headers, files, params), String.class);
}

/**
* 表单 - Query
* 上传多个文件且带参数的 Post 请求
*
* @param headers 参数
* @param url 参数
* @param files 参数
* @param params 参数
* @return 返回
*/
public static ResponseEntity<String> executePostFromFileParam(HttpHeaders headers, String url, List<File> files, Map<String, String> params) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
return restTemplate.postForEntity(url, getMultiValueMap(headers, files, params), String.class);
}

private static HttpEntity<MultiValueMap<String, Object>> getMultiValueMap(HttpHeaders headers, List<File> files, Map<String, String> params) {
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
if (files != null && files.size() > 0) {
headers.set("Content-Type", "multipart/form-data");
for (File file : files) {
multiValueMap.add("file", new org.springframework.core.io.FileSystemResource(file));
}
} else {
headers.set("Content-Type", "application/x-www-form-urlencoded");
}
if (params != null && params.size() > 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
multiValueMap.add(entry.getKey(), entry.getValue());
}
}
return new HttpEntity<>(multiValueMap, headers);
}


/**
* Body请求 - Body
* @param headers 参数
* @param url 参数
* @return 返回
*/
public static ResponseEntity<String> executePostBody(HttpHeaders headers, String url) {
return executePostBodyParam(headers,url,null);
}

/**
* Body请求 - Body
* @param headers 参数
* @param url 参数
* @param str 参数
* @return 返回
*/
public static ResponseEntity<String> executePostBodyParam(HttpHeaders headers, String url,String str) {
RestTemplate restTemplate = ApplicationContextHelper.getBean(RestTemplate.class);
headers.set("Content-Type", "application/json;charset=UTF-8");
HttpEntity<String> httpEntity = new HttpEntity<>(str, headers);
return restTemplate.postForEntity(url, httpEntity, String.class);
}

}
完
RedisTemplate 封装工具类
CentOS7防火墙操作命令

本文标题:RestTemplate 封装工具类

文章作者:十二

发布时间:2021-07-02 10:54:00

最后更新:2021-07-23 13:15:52

原始链接:https://www.zhuqiaolun.com/2021/07/1625194440104/1625194440104/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx6
  • Kafka4
  • Linux2
  • Logger6
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot16
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 一月 20261
  • 十二月 20253
  • 八月 20252
  • 六月 20251
  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:103.2k
本站文章数量统计:139
© 2026 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github