用 WebClient 代替 RestTemplate
RestTemplate是用于执行 HTTP 请求的同步客户端,通过底层 HTTP 客户端库(例如 JDK HttpURLConnection、Apache HttpComponents 等)公开一个简单的模板方法 API。
RestTemplate 通过 HTTP 方法为常见场景提供模板
注意:从Spring 5.0开始,该类处于维护模式,只有较小的更改请求和错误被接受。 请考虑使用 org.springframework.web.reactive.client.WebClient,它具有更现代的 API 并支持同步、异步和流式处理方案。

WebClient是用于执行 HTTP 请求的非阻塞、响应式客户端,通过底层 HTTP 客户端库(如 Reactor Netty)公开流畅的响应式 API。

平时在Spring Boot项目开发过程中,可以多使用WebClient来进行异步远程调用
举个例子:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
package com.example.demo413;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
/**
* @Author ChengJianSheng
* @Date 2022/4/13
*/
@RestController
public class HelloController {
private String url = "http://localhost:8093/soas-enterprise/hello/hi?name=zhangsan"; // Thread.sleep(5000)
/**
* WebClient
*/
@GetMapping("/hello")
public String hello() {
Mono resp = WebClient.create().get().uri(url).retrieve().bodyToMono(String.class);
resp.subscribe(e-> System.out.println(e)); // 后执行
System.out.println(1); // 先执行
return "hello";
}
/**
* RestTemplate
*/
@GetMapping("/hi")
public String hi() {
RestTemplate restTemplate = new RestTemplate();
String resp = restTemplate.getForEntity(url, String.class).getBody();
System.out.println(resp); // 先执行
System.out.println(1); // 后执行
return resp;
}
}
观察控制台,看代码执行顺序
用 WebClient 代替 RestTemplate的更多相关文章
- Spring WebClient vs. RestTemplate
1. 简介 本教程中,我们将对比 Spring 的两种 Web 客户端实现 -- RestTemplate 和 Spring 5 中全新的 Reactive 替代方案 WebClient. 2. 阻塞 ...
- Spring5 of WebClient(转载)
前言 Spring5带来了新的响应式web开发框架WebFlux,同时,也引入了新的HttpClient框架WebClient.WebClient是Spring5中引入的执行 HTTP 请求的非阻塞. ...
- Feign Client 原理和使用
Feign Client 原理和使用 一块石头 公众号:好奇心森林 关注他 创作声明:内容包含虚构创作 6 人赞同了该文章 最近一个新项目在做后端HTTP库技术选型的时候对比了Spring We ...
- Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)
通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...
- SpringBoot26 RestTemplate、WebClient
1 RestTemplate RestTemplate是在客户端访问 Restful 服务的一个核心类:RestTemplate通过提供回调方法和允许配置信息转换器来实现个性化定制RestTempla ...
- SpringBoot系列: RestTemplate 快速入门
====================================相关的文章====================================SpringBoot系列: 与Spring R ...
- RestTemplate的使用介绍汇总
一 常用方法 https://blog.csdn.net/u012843361/article/details/79893638 二 关于client的选择和设置(通过设置ClientHttpRequ ...
- SpringCloud03 Ribbon知识点、 Feign知识点、利用RestTemplate+Ribbon调用远程服务提供的资源、利用feign调用远程服务提供的资源、熔断
1 远程服务资源的调用 1.1 古老的套路 在微服务出现之前,所有的远程服务资源必须通过RestTemplate或者HttpClient进行:但是这两者仅仅实现了远程服务资源的调用,并未提供负载均衡实 ...
- RestTemplate最详解
目录 1. RestTemplate简单使用 2. 一些其他设置 3. 简单总结 在项目中,当我们需要远程调用一个HTTP接口时,我们经常会用到RestTemplate这个类.这个类是Spring框架 ...
- Spring框架spring-web模块中的RestTemplate类详解
RestTemplate类是spring-web模块中进行HTTP访问的REST客户端核心类.RestTemplate请求使用阻塞式IO,适合低并发的应用场景. 1. RestTemplate类提供了 ...
随机推荐
- 05-Shell索引数组变量
1.介绍 Shell 支持数组(Array),数组是若干数据的集合,其中的每一份数据都称为数组的元素. 注意Bash Shell 只支持一维数组,不支持多维数组. 2.数组的定义 2.1 语法 在 S ...
- -- spi flash 擦除接口调用HAL库不同函数的区别
[描述] 在使用STM32F429操作W25Q128时,为验证flash工作正常,做简单的读写数据校验,在擦除接口中使用 HAL_SPI_Transmit 方法一直工作异常,使用 HAL_SPI_Tr ...
- JS - console多个值
Promise.all([p1, p2]).then( (res) => { let [p1, p2] = res; console.l ...
- ONVIF网络摄像头(IPC)客户端开发—RTSP RTCP RTP加载AAC音频流
前言: RTSP,RTCP,RTP一般是一起使用,在FFmpeg和live555这些库中,它们为了更好的适用性,所以实现起来非常复杂,直接查看FFmpeg和Live555源代码来熟悉这些协议非常吃力, ...
- [转帖]Tomcat部署及优化
目录 一.Tomcat简介 1 Tomcat的三大核心组件 2 Java Servlet 3 JSP全称Java Server Pages 4 Tomcat 功能组件结构 5 Tomcat 请求过程 ...
- [转帖]Linux系统NVME盘分区和挂载
https://www.jianshu.com/p/04327f1b97cb 查看系统里面识别到的硬盘和分区的信息 $ sudo fdisk -l Disk /dev/nvme1n1: 1.8 TiB ...
- [转帖]Jmeter学习笔记(二十三)——生成HTML性能报告
https://www.cnblogs.com/pachongshangdexuebi/p/11759316.html 有时候我们写性能报告的时候需要一些性能分布图,JMeter是可以生成HTML性能 ...
- Python学习之六_同时访问Oracle和Mysql的方法
Python学习之六_同时访问Oracle和Mysql的方法 背景 jaydebeapi 可以访问大部分数据库. 但是他有一个问题是仅能够访问一种类型的数据库. 如果同事连接两种数据库,那么就会出现问 ...
- gcore的学习
gcore的学习-解决jmap无法生成dump文件的一种方法 背景 周末在跆拳道馆看孩子练跆拳道. 开着笔记本翻到了 扣钉日记 公众号里面的讲解 想着自己也遇到过无法保存dump文件的情况. 所以想学 ...
- [转帖]简单理解Linux的Memory Overcommit
https://zhuanlan.zhihu.com/p/551677956 Memory Overcommit的意思是操作系统承诺给进程的内存大小超过了实际可用的内存.一个保守的操作系统不会允许me ...