180815-Spring之RestTemplate中级使用篇

Spring之RestTemplate中级使用篇
前面一篇介绍了如何使用RestTemplate发起post和get请求,然而也只能满足一些基本的场景,对于一些特殊的如需要设置请求头,添加认证信息等场景,却没有提及可以怎么做,这一篇则相当于进阶版,将主要介绍
- get/post请求如何携带 header
- post传文件可以怎么玩, post提交json串怎么处理
exchange方法的使用姿势
I. 请求头设置
首先一个问题就是为什么要设置请求头?
我们通过浏览器正常访问的接口,可能通过代码直接访问时,就会提示403
而这样的原因,较多的一个可能就是后端的请求做了限制,比如根据请求的agent,判断是否为爬虫;根据referer判断是否要返回数据等等;而后端进行校验的条件中,往往会拿请求头的数据,因此这也就要求我们在使用时,主动的塞入一些请求头信息
1. Get请求
直接看RestTemplate提供的几个Get请求接口,并没有发现有设置请求头的地方,是不是就表明没法设置请求头了?
答案档案是能设置了,具体的使用思路有点类似mvc中的拦截器,自定义一个拦截器,然后在你实际发起请求时,拦截并设置request的请求头
注意到 RestTemplate 的父类InterceptingHttpAccessor提供了一个接收Interceptor的接口org.springframework.http.client.support.InterceptingHttpAccessor#setInterceptors,这个就是我们所需要的关键点(讲道理,除非事先就知道有这么个玩意,不然真让你去找,还不一定能找到)
所以第一步就是写一个ClientHttpRequestInterceptor类,添加请求头
public class UserAgentInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
HttpHeaders headers = request.getHeaders();
headers.add(HttpHeaders.USER_AGENT,
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
return execution.execute(request, body);
}
}
下一步就是在创建RestTemplate对象之后,声明解释器并测试使用了
@Test
public void testGetHeader() {
String url = "http://localhost:8080/agent?name=一灰灰Blog";
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(new UserAgentInterceptor()));
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
System.out.println(response.getStatusCode() + " | " + response.getBody());
}
首先在测试之前,先搭一个服务,简单判断agent,不满足条件的直接403, 后端mock代码如下
@ResponseBody
@RequestMapping(path = "agent")
public String agent(HttpServletRequest request, HttpServletResponse response,
@RequestParam(value = "name", required = false) String name) throws IOException {
String agent = request.getHeader(HttpHeaders.USER_AGENT);
if (StringUtils.isEmpty(agent) || !agent.contains("WebKit")) {
response.sendError(403, " illegal agent ");
}
return "welcome " + name;
}
上面执行后输出如下,添加请求头后正常返回

当然也需要对比下不设置agent的情况了,直接抛了一个异常出来了(说明,不显示覆盖User-Agent时,后端接收到的agent为: Java/1.8.0_171

上面虽然只给了设置User-Agent的例子,但是其他的请求头,都是可以放在自定义的Interceptor中添加进去的
2. Post请求
当然get请求使用的这种姿势,对于post而言或者对于其他的http请求方法而言,都是通用的,而对于post请求来说,还有另外一种方式,就是requset参数,可以携带request headers
首先mock一个后端接口
@ResponseBody
@RequestMapping(path = "post", method = {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST},
produces = "charset/utf8")
public String post(HttpServletRequest request, HttpServletResponse response,
@RequestParam(value = "email", required = false) String email,
@RequestParam(value = "nick", required = false) String nick) throws IOException {
String agent = request.getHeader(HttpHeaders.USER_AGENT);
if (StringUtils.isEmpty(agent) || !agent.contains("WebKit")) {
response.sendError(403, " illegal agent ");
return null;
}
return "success email=" + email + "&nick=" + URLEncoder.encode(nick, "UTF-8") + "&status=success";
}
简单的使用姿势如下
@Test
public void testPostHeader() {
String url = "http://localhost:8080/post";
String email = "test@hhui.top";
String nick = "一灰灰Blog";
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("email", email);
params.add("nick", nick);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
System.out.println(response.getStatusCode() + " | " + response.getBody());
}
从上面代码可以看出,具体的使用姿势相比于不添加请求头时,只是多了一个封装
- 具体的header信息分装到
HttpHeaders对象中 - 请求参数依然封装到
MultiValueMap中 - 然后根据请求头 + 请求参数,构建
HttpEntity对象,将这个作为post的请求request参数传入

当然作为对比,当不加入headers时,看下返回什么鬼, 406异常,但是我们后端定义的是403,为什么会返回406呢?

3. exchange 方式
另外还会关注到RestTemplate还提供了一个exchange方法,这个相当于一个公共的请求模板,使用姿势和get/post没有什么区别,只是可以由调用发自己来选择具体的请求方法
使用exchange对上面的post请求进行简单的替换如下, 基本上除了多一个参数之外没有什么区别了
@Test
public void testPostHeader() {
String url = "http://localhost:8080/post";
String email = "test@hhui.top";
String nick = "一灰灰Blog";
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("email", email);
params.add("nick", nick);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
System.out.println(response.getStatusCode() + " | " + response.getBody());
}
那么问题来了,为什么要有这个东西?或者说这个接口的提供可以带来什么好处?
- 当你写一个公共的Rest工具类时,就比较方便了,底层统一,具体的方法由上层业务方选择即可
- get可以通过这种方式直接添加请求头(也就是不需要第一种case中的自定义拦截器来塞入header,显然更加灵活)
II. Post参数提交
前面的post参数提交,其实默认采用的是 application/x-www-form-urlencoded 方式,即是我们最常见的表单提交方式,在浏览器中的表现形式如下

此外,还有一种直接提交json串的方式,在前文 《180730-Spring之RequestBody的使用姿势小结》中有说明,具体浏览器中表现形式为

所以接下来的问题就是,RestTemplate要怎么处理呢?
1. json串提交
建议在看下面的内容之前,先看一下上面的那篇博文,理解下RequestBody是什么东西
首先搭建一个后端
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Req {
private String key;
private Integer size;
}
@ResponseBody
@RequestMapping(value = "/body", method = {RequestMethod.POST, RequestMethod.GET, RequestMethod.OPTIONS})
public String body(@RequestBody Req req) {
return req.toString();
}
然后使用方式,无非就是在请求头中添加下Content-Type为Application/json
@Test
public void testPostRequestBody() {
String url = "http://localhost:8080/body";
String email = "test@hhui.top";
String nick = "一灰灰Blog";
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("nick", nick);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
HttpEntity<Map<String, String>> request = new HttpEntity<>(params, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
System.out.println(response.getStatusCode() + " | " + response.getBody());
}
注意下post参数,是放在Map容器中,而不是之前的MultiValueMap
运行时截图如下

2. 文件上传
post除了传表单数据(json串)之外,还有一个常见的就是上传文件了,实际上使用RestTemplate来实现文件上传,算是比较简单的了,和前面的使用基本上也没有什么差别,只是将文件作为params参数而已
首先搭建一个Controller后端服务,简单的获取文件内容,并返回
@ResponseBody
@PostMapping(path = "file")
public String file(MultipartHttpServletRequest request) throws IOException {
MultipartFile file = request.getFile("file");
if (file == null) {
return "no file!";
}
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = reader.readLine();
while (line != null) {
builder.append(line);
line = reader.readLine();
}
reader.close();
return builder.toString();
}
然后就是实际的测试用例,将文件包装在FileSystemResource对象中,然后塞入MultiValueMap中,注意下面的使用并没有显示添加请求头,而这种时候,content-type 通过断点查看实际为 content-type = multipart/form-data;
@Test
public void testPostFile() {
String url = "http://localhost:8080/file";
FileSystemResource resource = new FileSystemResource(new File("/tmp/test.txt"));
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("file", resource);
params.add("fileName", "test.txt");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url, params, String.class);
System.out.println(response);
}

III. 小结
本篇主要介绍如何给RestTemplate发起的请求,添加请求头,以及完成某些特定的请求,下面小结一下使用姿势
1. 设置header
两种方式
- 一个是设置
Interceptors,在拦截器中主动添加上对应的请求头即可,适用于为所有的请求添加统一的请求头的场景- 这种方式不仅仅能用来设置请求头,还可以在其中做很多其他的事情
- 另外一种方式针对
postForXXX和exchange两种请求方式而言,同样自己设置请求头HttpHeader,然后将请求头和params封装到HttpEntity,作为request参数提交即可
2. 特殊的请求方式
json串的提交
- 设置请求头的content-type为
Applicaiton/json - 将请求的数据封装到map容器内(或者直接定义一个请求参数的DTO对象也可以)
- 然后将header和参数封装到
HttpEntity中,发起请求即可
文件上传
- 将资源文件塞入到
MultiValueMap中即可,和普通的请求方式没有什么区别
3. 其他
初级篇介绍了如何使用RestTemplate发起简单的GET/POST请求;
中级篇则介绍请求的过程中添加设置请求头,以及某些特殊的请求可以怎么处理
显然还会有高级篇,除了上面的东西,我们还需要知道些什么呢?
- 请求超时的设置比较实用,有必要了解下
- 在访问某些特殊的网站时,代理的设置也避不开
- 请求有身份鉴权的情况下,如何安全的携带自己的身份呢?
- RestTemplate底层使用的是什么网络库做的网络访问?可以用其他的进行替换么?(答案肯定是可以,不然这个命名就标准的名不副实了)
关于高级篇,坐等更新
IV. 其他
0. 相关博文
1. 一灰灰Blog: https://liuyueyi.github.io/hexblog
一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
2. 声明
尽信书则不如,已上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
- 微博地址: 小灰灰Blog
- QQ: 一灰灰/3302797840
3. 扫描关注
一灰灰blog

知识星球

180815-Spring之RestTemplate中级使用篇的更多相关文章
- Spring Cloud 入门 之 Hystrix 篇(四)
原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...
- Spring Cloud 入门 之 Feign 篇(三)
原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...
- Spring Cloud 入门 之 Ribbon 篇(二)
原文地址:Spring Cloud 入门 之 Ribbon 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Cloud 入门 之 Eureka ...
- Spring Cloud 入门 之 Eureka 篇(一)
原文地址:Spring Cloud 入门 之 Eureka 篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Cloud 是一系列框架的有序集合.它利用 Sp ...
- Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇
Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...
- 精讲RestTemplate第2篇-多种底层HTTP客户端类库的切换
本文是精讲RestTemplate第2篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 RestTemplate只是对其他的HTTP客 ...
- 精讲RestTemplate第3篇-GET请求使用方法详解
本文是精讲RestTemplate第3篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- 精讲RestTemplate第4篇-POST请求方法使用详解
本文是精讲RestTemplate第4篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- 精讲RestTemplate第4篇-DELETE、PUT等请求方法使用详解
本文是精讲RestTemplate第5篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
随机推荐
- 获取索引--------用range()和len()
a = ['Google', 'Baidu', 'Runoob', 'Taobao', 'QQ'] for i in range(len(a)): print(i+1,a[i])
- 面向对象程序设计__Task3_Calculator
The initial part of the Calculator program 题目链接:Click Here github链接:Click Here 看到这个题目的话,想到就是有3个任务要去做 ...
- Alpha 冲刺报告(6/10)
Alpha 冲刺报告(6/10) 队名:洛基小队 峻雄(组长) 已完成:实现角色的移动. 明日计划:关于角色的属性设计. 剩余任务:角色的属性脚本 困难:角色的属性以及具体的编码 ---------- ...
- 【原创】python requests 库底层Sockets处于close_wait状态
以前对于Requests库只是简单是使用,在现在公司的后台中,有多个接口是直接使用requests.get .post之类的方法来做的,进行过一段时间的压力测试,发现性能低的可怜,且linux服务器有 ...
- rimraf命令 递归删除目录所有文件
使用webpack build文件项目时每次都会生成一个dist目录,有时需要把dist目录里的所以旧文件全部删掉, 除了可以使用rm -rf /dist/命令删除外,还可以使用rimraf /dis ...
- 【模板】.bat对拍
对拍是个很有用的东西,比如在验证贪心策略是否正确时,可以写上个暴力然后和贪心程序对拍上几个小时. 在c++里用system写对拍总是会出现一些莫名其妙的问题.. 比如my.out明明是1 fc的时候却 ...
- tomcat服务器宕机解决方案
报错信息: java.lang.Object.wait(Native Method) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:1 ...
- B. A Leapfrog in the Array
http://codeforces.com/problemset/problem/949/B Dima is a beginner programmer. During his working pro ...
- Kafka集群配置
kafka_2.11-0.9.0.1.tgz 1.进入项目前的目录 cd /home/dongshanxia mkdir kafka #创建项目目录 cd kafka #进入项目目录 mkdir ka ...
- 腾讯云Mac图床插件
背景 随着博客越写越多,难免会遇到需要插入图片来说明的情况. 图床选择 首先调研了市面上的图床服务,本着稳定长期的目标,过滤掉了打一枪换一个地方的野鸡小网站,剩余比较靠谱的优缺点如下. 图床 优点 缺 ...