180815-Spring之RestTemplate中级使用篇

logo

Spring之RestTemplate中级使用篇

前面一篇介绍了如何使用RestTemplate发起post和get请求,然而也只能知足一些基本的场景,对于一些特殊的如须要设置请求头,添加认证信息等场景,却没有说起能够怎么作,这一篇则至关于进阶版,将主要介绍java

  • get/post请求如何携带 header
  • post传文件能够怎么玩, post提交json串怎么处理
  • exchange方法的使用姿式

<!-- more -->git

I. 请求头设置

首先一个问题就是为何要设置请求头?github

咱们经过浏览器正常访问的接口,可能经过代码直接访问时,就会提示403spring

而这样的缘由,较多的一个可能就是后端的请求作了限制,好比根据请求的agent,判断是否为爬虫;根据referer判断是否要返回数据等等;然后端进行校验的条件中,每每会拿请求头的数据,所以这也就要求咱们在使用时,主动的塞入一些请求头信息json

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对象以后,声明解释器并测试使用了mvc

@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/he...

一灰灰的我的博客,记录全部学习和工做中的博文,欢迎你们前去逛逛

2. 声明

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

3. 扫描关注

一灰灰blog

QrCode

知识星球

zhishi

相关文章
相关标签/搜索