RestTmplate

2023-09-22 10:36:37

why

发送 http 请求,估计很多人用过 httpclient 和 okhttp,确实挺好用的,而 Spring web 中的 RestTemplate 和这俩的功能类似,也是用来发送 http 请求的,不过用法上面比前面的 2 位要容易很多。

spring 框架提供的 RestTemplate 类可用于在应用中调用 rest 服务,它简化了与 http 服务的通信方式,统一了 RESTful 的标准,封装了 http 链接, 我们只需要传入 url 及返回值类型即可。相较于之前常用的 HttpClient,RestTemplate 是一种更优雅的调用 RESTful 服务的方式。

what

发送http请求,支持POST、GET、DELETE、PUT、OPTION请求

how

三种调用方式(forObject\forEntity\exchange)

@Test
public void test1() {
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8080/chat16/test/get";
    //getForObject方法,获取响应体,将其转换为第二个参数指定的类型
    BookDto bookDto = restTemplate.getForObject(url, BookDto.class);
    System.out.println(bookDto);
}

@Test
public void test2() {
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8080/chat16/test/get";
    //getForEntity方法,返回值为ResponseEntity类型
    // ResponseEntity中包含了响应结果中的所有信息,比如头、状态、body
    ResponseEntity<BookDto> responseEntity = restTemplate.getForEntity(url, BookDto.class);
    //状态码
    System.out.println(responseEntity.getStatusCode());
    //获取头
    System.out.println("头:" + responseEntity.getHeaders());
    //获取body
    BookDto bookDto = responseEntity.getBody();
    System.out.println(bookDto);
}

@Test
public void test5() {
    RestTemplate restTemplate = new RestTemplate();
    //返回值为泛型
    String url = "http://localhost:8080/chat16/test/getList";
    //若返回结果是泛型类型的,需要使用到exchange方法,
    //这个方法中有个参数是ParameterizedTypeReference类型,通过这个参数类指定泛型类型
    ResponseEntity<List<BookDto>> responseEntity =
            restTemplate.exchange(url,
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<List<BookDto>>() {
                    });
    List<BookDto> bookDtoList = responseEntity.getBody();
    System.out.println(bookDtoList);
}

动态URL参数

@Test
public void test3() {
    RestTemplate restTemplate = new RestTemplate();
    //url中有动态参数
    String url = "http://localhost:8080/chat16/test/get/{id}/{name}";
    Map<String, String> uriVariables = new HashMap<>();
    uriVariables.put("id", "1");
    uriVariables.put("name", "SpringMVC系列");
    //使用getForObject或者getForEntity方法
    BookDto bookDto = restTemplate.getForObject(url, BookDto.class, uriVariables);
    System.out.println(bookDto);
}

下载小文件

@Test
public void test6() {
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8080/chat16/test/downFile";
    //文件比较小的情况,直接返回字节数组
    ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(url, byte[].class);
    //获取文件的内容
    byte[] body = responseEntity.getBody();
    String content = new String(body);
    System.out.println(content);
}

如果文件大的时候,这种方式就有问题了,会导致 oom,要用下面的方式了。

下载大文件

文件比较大的时候,比如好几个 G,就不能返回字节数组了,会把内存撑爆,导致 OOM,需要使用 execute 方法了,这个方法中有个 ResponseExtractor 类型的参数,restTemplate 拿到结果之后,会回调{@link ResponseExtractor#extractData}这个方法,在这个方法中可以拿到响应流,然后进行处理,这个过程就是变读边处理,不会导致内存溢出

@Test
public void test7() {
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8080/chat16/test/downFile";
    String result = restTemplate.execute(url,
            HttpMethod.GET,
            null,
            new ResponseExtractor<String>() {
                @Override
                public String extractData(ClientHttpResponse response) throws IOException {
                    System.out.println("状态:"+response.getStatusCode());
                    System.out.println("头:"+response.getHeaders());
                    //获取响应体流
                    InputStream body = response.getBody();
                    //处理响应体流
                    String content = IOUtils.toString(body, "UTF-8");
                    return content;
                }
            }, new HashMap<>());

    System.out.println(result);
}

Content-Type

http 请求头中的 Content-Type 用来指定请求的类型,常见的有 3 种

Content-Type说明
application/x-www-form-urlencoded页面中普通的 form 表单提交时就是这种类型,表单中的元素会按照名称和值拼接好,然后之间用&连接,格式如:p1=v1&p2=v2&p3=v3然后通过 urlencoded 编码之后丢在 body 中发送
multipart/form-data页面中表单上传文件的时候,用到的就是这种格式
application/json将发送的数据转换为 json 格式,丢在 http 请求的 body 中发送,后端接口通常用@RequestBody 配合对象来接收。

扩展

RestTemplate 内部默认用的是 jdk 自带的 HttpURLConnection 发送请求的,性能上面并不是太突出。可以将其替换为 httpclient 或者 okhttp。

HttpClient

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.7</version>
</dependency>
public HttpClient httpClient() {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    try {
        //设置信任ssl访问
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (arg0, arg1) -> true).build();
        httpClientBuilder.setSSLContext(sslContext);
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                // 注册http和https请求
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslConnectionSocketFactory).build();

        //使用Httpclient连接池的方式配置(推荐),同时支持netty,okHttp以及其他http框架
        PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        // 最大连接数
        poolingHttpClientConnectionManager.setMaxTotal(1000);
        // 同路由并发数
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
        //配置连接池
        httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
        // 重试次数
        httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, true));
        //设置默认请求头
        List<Header> headers = new ArrayList<>();
        httpClientBuilder.setDefaultHeaders(headers);
        return httpClientBuilder.build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public ClientHttpRequestFactory clientHttpRequestFactory() {
    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient());
    // 连接超时(毫秒),这里设置10秒
    clientHttpRequestFactory.setConnectTimeout(10 * 1000);
    // 数据读取超时时间(毫秒),这里设置60秒
    clientHttpRequestFactory.setReadTimeout(60 * 1000);
    // 从连接池获取请求连接的超时时间(毫秒),不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的
    clientHttpRequestFactory.setConnectionRequestTimeout(10 * 1000);
    return clientHttpRequestFactory;
}

public RestTemplate restTemplate(){
    //创建RestTemplate的时候,指定ClientHttpRequestFactory
    return new RestTemplate(this.clientHttpRequestFactory());
}

@Test
public void test18() {
    RestTemplate restTemplate = this.restTemplate();
    String url = "http://localhost:8080/chat16/test/get";
    //getForObject方法,获取响应体,将其转换为第二个参数指定的类型
    BookDto bookDto = restTemplate.getForObject(url, BookDto.class);
    System.out.println(bookDto);
}

okhttp

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.3.1</version>
</dependency>
new RestTemplate(new OkHttp3ClientHttpRequestFactory());
更多推荐

DA5 网站用户没有补全的信息

目录1.题目描述2.输入描述3.输出描述4.题目分析5.通过代码1.题目描述现有一个Nowcoder.csv文件,它记录了牛客网的部分用户数据,包含如下字段(字段与字段之间以逗号间隔):Nowcoder_ID:用户IDLevel:等级Achievement_value:成就值Num_of_exercise:刷题量Gra

Navicat安装使用教程

众所周知,Navicat是一款轻量级的用于MySQL连接和管理的工具,非常好用,使用起来方便快捷,简洁。下面我会简单的讲一下其安装以及使用的方法。并且会附带相关的永久安装教程。简介一般我们在开发过程中是离不开数据库的,Navicat是一款轻量级的用于MySQL连接和管理的工具,非常好用。https://note.you

redis -- 基本介绍 -- 字符串、列表、集合、有序集合、哈希

目录Redis字符串string列表list集合set有序集合sortedset哈希hashRedisRedis(RemoteDictionaryServer)是一个开源的、高性能的、内存中的数据存储系统他可以用作数据库缓存和消息队列等各种场景是目前最热门的NoSQL数据库之一MySQL的磁盘IO读写速度与内存相比非常

直线模组的常用语

在工业生产中,直线模组的叫法有很多种,对于新手小白来说,很容易就会被绕晕,今天我们就来简单说一下直线模组的常用称呼吧!1、直线模组:与直线滑台同义,基本可以相互互换。直线模组一般是指可以完成直线运动和直线定位的传动模块,一般不包括电机驱动和运动控制系统在内,常用的直线模组分为滚珠丝杆型直线模组和同步带传动型直线模组。2

Windows 修改系统默认字体

WindowsRegistryEditorVersion5.00;重装机后电脑屏幕及字体调整.reg.lnk;;显示器分辨率:3840*2160;;自定义缩放:266;;辅助功能-文本大小-110%;;最后ClearType文本调谐器;https://www.cnblogs.com/bolang100/p/854804

数据结构 - 链表

线性表的链式存储结构概念将线性表L=(a0,a1,…,an-1)中各元素分布在存储器的不同存储块,成为结点,通过地址或指针建立元素之间的联系。结点的data域存放数据元素ai,而next域是一个指针,指向ai的直接后继ai+1所在的结点。下图中的首元结点(头结点)A的data不重要,next域指向链表的真正的第一个结点

js同级弹窗实现数据传输修改

window.postMessage是一种用于实现跨窗口通信的HTML5特性。它允许在不同窗口或iframe之间安全地传递数据,即使这些窗口来自不同的域名。window.postMessage方法接受两个参数:message:要发送的消息,可以是一个字符串或一个对象。targetOrigin:指定接收消息的窗口的源(o

C语言每日一题(7):获得月份天数

文章主题:获得月份天数🔥所属专栏:C语言每日一题📗作者简介:每天不定时更新C语言的小白一枚,记录分享自己每天的所思所想😄🎶个人主页:[₽]的个人主页🏄🌊目录前言编程起因项目介绍设计思路1.整体逻辑2.具体逻辑代码展示效果展现结语前言编程起因最近在牛客网上刷到了一个很好的训练分支语句的题目,于是想出了求两个数

NeRF-RPN:一个通用的目标检测框架

论文标题:NeRF-RPN:AgeneralframeworkforobjectdetectioninNeRFs代码:https://github.com/lyclyc52/NeRF_RPN图1:在NeRF上的Regionproposal结果视频演示效果:https://www.youtube.com/watch?v=

进程与线程

1进程1.1进程的概念进程就是正在运行的程序,它代表了程序所占用的内存区域1.2进程的特点独立性进程是系统中独立存在的实体,它可以拥有自己独立的资源,每个进程都拥有自己私有的地址空间在没有经过进程本身允许的情况下,一个用户进程不可以直接访问其他进程的地址空间动态性进程与程序的区别在于.程序只是一个静态的指令集合,而进程

spring security为啥是个垃圾框架?

古时候写代码,权限这块写过一个库,基本就是一个泛型接口,里面有几个方法:如验证输入的principal和credentials,返回token和authorities和roles,role就是一堆authorities集,也就说就是返回一堆authorities。然后每次请求会拿token找到authorities,然

热文推荐