180815-Spring之RestTemplate中级使用篇

logo

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;
}

上面执行后输出如下,添加请求头后正常返回

C420EE9FB481154F53D442684F8A7B9A.jpg

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

CA4B422728A6868A4366F7FE65F22BE6.jpg

上面虽然只给了设置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参数传入

2D203A36995BE818CECBD936F535875F.jpg

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

B6C5FDF670E826D8040F6B5EDBB21F30.jpg

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 方式,即是我们最常见的表单提交方式,在浏览器中的表现形式如下

body

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

json

所以接下来的问题就是,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

运行时截图如下

D5E1ABAFBD02418EE63B38C5061685FF.jpg

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);
}

14D0A3473CDF5DE85CB27FBD4DB3277F.jpg

III. 小结

本篇主要介绍如何给RestTemplate发起的请求,添加请求头,以及完成某些特定的请求,下面小结一下使用姿势

1. 设置header

两种方式

  • 一个是设置Interceptors,在拦截器中主动添加上对应的请求头即可,适用于为所有的请求添加统一的请求头的场景
    • 这种方式不仅仅能用来设置请求头,还可以在其中做很多其他的事情
  • 另外一种方式针对 postForXXXexchange 两种请求方式而言,同样自己设置请求头HttpHeader,然后将请求头和params封装到HttpEntity,作为request参数提交即可

2. 特殊的请求方式

json串的提交

  • 设置请求头的content-type为 Applicaiton/json
  • 将请求的数据封装到map容器内(或者直接定义一个请求参数的DTO对象也可以)
  • 然后将header和参数封装到 HttpEntity 中,发起请求即可

文件上传

  • 将资源文件塞入到MultiValueMap中即可,和普通的请求方式没有什么区别

3. 其他

初级篇介绍了如何使用RestTemplate发起简单的GET/POST请求;

中级篇则介绍请求的过程中添加设置请求头,以及某些特殊的请求可以怎么处理

显然还会有高级篇,除了上面的东西,我们还需要知道些什么呢?

  • 请求超时的设置比较实用,有必要了解下
  • 在访问某些特殊的网站时,代理的设置也避不开
  • 请求有身份鉴权的情况下,如何安全的携带自己的身份呢?
  • RestTemplate底层使用的是什么网络库做的网络访问?可以用其他的进行替换么?(答案肯定是可以,不然这个命名就标准的名不副实了)

关于高级篇,坐等更新

IV. 其他

0. 相关博文

1. 一灰灰Bloghttps://liuyueyi.github.io/hexblog

一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

2. 声明

尽信书则不如,已上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

3. 扫描关注

一灰灰blog

QrCode

知识星球

zhishi

posted @ 2018-08-15 20:14  一灰灰Blog  阅读(445)  评论(0编辑  收藏  举报