Spring 框架中发送 http 请求 —— RestTemplate

对四年前的文章重新做了下修改

简介

这是由 Spring 提供的网络请求工具,它内嵌在 spring-web

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.12</version>
    </dependency>

当然我们常用的 spring-boot-starter-web 也是包含它的,所以就不需要像这里一样显式引用了,其他细节可参见 RestTemplate 官方文档

使用方法

注意事项

RestTemplate 默认行为,如果请求出去未返回 200 的 http 状态码,会抛出异常,我们通常希望抛出异常交由请求方自行决定,此时我们需要覆盖默认的 ResponseErrorHandler ,下面为用什么也不干覆盖默认处理机制

        RestTemplate restTemplate = new RestTemplate();
        ResponseErrorHandler responseErrorHandler = new ResponseErrorHandler() {
            @Override
            public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
                return true;
            }

            @Override
            public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {

            }
        };
        restTemplate.setErrorHandler(responseErrorHandler);

便捷 API

// 此处含义:向 url 发送 Get 类型请求,将请求返回的结果反序列化成 String 类型
restTemplate.getForObject(url, String.class);

其他 postForObject 等类似换汤不换药 API 也是如此,通常我们不去使用,它们不够自由,无法设置编码格式等,乱码等问题无法解决,不再赘述,后面只将通用方案

通用方案

restTemplate.exchange() 它包含一个 HttpMethod 的入参,该参数决定了我们的请求是 GetPost …… 中的哪一种

画外音: 顺便提一嘴原生的 RestTemplate 其实并不支持 Patch 类型的请求,可以引入第三方实现来扩展 RestTemplate 功能

示例

application/json 请求

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://www.testXXX.com";
        /* 注意:必须 http、https……开头,不然报错,浏览器地址栏不加 http 之类不出错是因为浏览器自动帮你补全了 */
        HttpHeaders headers = new HttpHeaders();
        /* 这个对象有add()方法,可往请求头存入信息 */
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        /* 解决中文乱码的关键 , 还有更深层次的问题 关系到 StringHttpMessageConverter,先占位,以后补全*/
        HttpEntity<String> entity = new HttpEntity<String>(body, headers);
        /* body是Http消息体例如json串 */
        restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        /*上面这句返回的是往 url发送 post请求 请求携带信息为entity时返回的结果信息 String.class 是可以修改的,其实本质上就是在指定反序列化对象类型,这取决于你要怎么解析请求返回的参数*/

application/x-www-form-urlencoded 请求

        RestTemplate restTemplate=new RestTemplate();
        /* 注意:必须 http、https……开头,不然报错,浏览器地址栏不加 http 之类不出错是因为浏览器自动帮你补全了 */
        String url="http://www.testXXX.com";
        String bodyValTemplate = "var1=" + URLEncoder.encode("测试数据1", "utf-8") + "&var2=" + URLEncoder.encode("test var2", "utf-8");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity entity = new HttpEntity(bodyValTemplate, headers);
        restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

另外此处 HttpEntity entity 构造过程替换成下面方式也是等效的

        LinkedMultiValueMap body=new LinkedMultiValueMap();
        body.add("var1","测试数据1");
        body.add("var2","test Val2");
        HttpEntity entity = new HttpEntity(body, headers);

等效于 Postman 这样的请求

传输文件 请求

    @PostMapping("/file2")
    public ResponseEntity<?> fileUpload2(@RequestParam("files") ArrayList<MultipartFile> filesList) throws IOException {
        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
        for (MultipartFile multipartFile : filesList) {
            Resource resource = new InputStreamResource(multipartFile.getInputStream()) {
                @Override
                public String getFilename() {
                    return multipartFile.getOriginalFilename();
                }

                @Override
                public long contentLength() {
                    return multipartFile.getSize();
                }
            };
            requestBody.add("files", resource);
        }
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE);
        HttpEntity<MultiValueMap<String, ?>> requestEntity = new HttpEntity<>(requestBody, httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.exchange("http://xxxxxx/file", HttpMethod.POST, requestEntity, String.class);
        return success(responseEntity.getBody());
    }

这是一个 Controller 层通过 RestTemplate 转发文件的示例,至于这段代码是怎么来的,请参见 官方文档 Multipart 的描述 和底部参考资料

简而言之,RestTemplate 原生通过 FormHttpMessageConverter 来支持 multipart/form-data 请求,这个过程中它认 Resource 对象作为请求的内容

FileSystemResource 符合条件,但需要一个真实文件存在于本地或远端,理想状态最好就在内存中存在,执行结束就丢弃,不在本地产生其他负作用,求其次就使用了 InputStreamResource 对象

接着追踪 FormHttpMessageConverter 代码你会发现,它依赖了 ResourcegetFilename()contentLength()方法,前者如果为 null 会忽略该文件的传输;后者则是因为默认实现是会去读流,而流只能读一次,这一次机会在计算文件长度上了就得不偿失

异步操作

AsyncRestTemplate 是用来进行异步网络请求操作的,但是在后续版本官方放弃了这个方案,推荐你使用 WebClient,这条路子可以说是寄了(4 年前我还天真的以为自己会来补那部分内容,毕竟当时 AsyncRestTemplate 没有被标记 Deprecated)

参考资料

通过RestTemplate上传文件(InputStreamResource详解)

HttpClient与CloseableHttpClient

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值