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的更多相关文章

  1. Spring WebClient vs. RestTemplate

    1. 简介 本教程中,我们将对比 Spring 的两种 Web 客户端实现 -- RestTemplate 和 Spring 5 中全新的 Reactive 替代方案 WebClient. 2. 阻塞 ...

  2. Spring5 of WebClient(转载)

    前言 Spring5带来了新的响应式web开发框架WebFlux,同时,也引入了新的HttpClient框架WebClient.WebClient是Spring5中引入的执行 HTTP 请求的非阻塞. ...

  3. Feign Client 原理和使用

    Feign Client 原理和使用 一块石头 ​ 公众号:好奇心森林 ​关注他 创作声明:内容包含虚构创作 6 人赞同了该文章 最近一个新项目在做后端HTTP库技术选型的时候对比了Spring We ...

  4. Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)

    通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...

  5. SpringBoot26 RestTemplate、WebClient

    1 RestTemplate RestTemplate是在客户端访问 Restful 服务的一个核心类:RestTemplate通过提供回调方法和允许配置信息转换器来实现个性化定制RestTempla ...

  6. SpringBoot系列: RestTemplate 快速入门

    ====================================相关的文章====================================SpringBoot系列: 与Spring R ...

  7. RestTemplate的使用介绍汇总

    一 常用方法 https://blog.csdn.net/u012843361/article/details/79893638 二 关于client的选择和设置(通过设置ClientHttpRequ ...

  8. SpringCloud03 Ribbon知识点、 Feign知识点、利用RestTemplate+Ribbon调用远程服务提供的资源、利用feign调用远程服务提供的资源、熔断

    1 远程服务资源的调用 1.1 古老的套路 在微服务出现之前,所有的远程服务资源必须通过RestTemplate或者HttpClient进行:但是这两者仅仅实现了远程服务资源的调用,并未提供负载均衡实 ...

  9. RestTemplate最详解

    目录 1. RestTemplate简单使用 2. 一些其他设置 3. 简单总结 在项目中,当我们需要远程调用一个HTTP接口时,我们经常会用到RestTemplate这个类.这个类是Spring框架 ...

  10. Spring框架spring-web模块中的RestTemplate类详解

    RestTemplate类是spring-web模块中进行HTTP访问的REST客户端核心类.RestTemplate请求使用阻塞式IO,适合低并发的应用场景. 1. RestTemplate类提供了 ...

随机推荐

  1. 基于Html+腾讯云播SDK开发的m3u8播放器

    周末业余时间在家无事,学习了一下腾讯的云播放sdk,并制作了一个小demo(m3u8播放器),该在线工具是基于腾讯的云播sdk开发的,云播sdk非常牛,可以支持多种播放格式. 预览地址 m3u8pla ...

  2. 京东App秒杀抢购流程接口分析(基于pypp技术)

    App数据抓包必需工具 必需工具:小米手机,Charles,HttpCanary 从2022年2月后,京东只限于从app发起抢购,所以,网上的很多工具已经无效了.只能分析app端的底层协议和流程. g ...

  3. 前端开发环境搭建踩坑笔记——npm install node-sass安装失败的解决方案

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  4. Clock Gating Design

    GPU max power distribution internal power and switch power - 动态功耗(时钟翻转) Leakage power - 漏电功耗(静态功耗,mo ...

  5. [转帖]auto_explain

    https://help.kingbase.com.cn/v8/development/sql-plsql/ref-extended-plug-in/auto_explain.html 6.1. 插件 ...

  6. [转帖]【OS】OSWbb(OSWatcher Black Box)的简介和使用

    http://blog.itpub.net/26736162/viewspace-2142613/ OSWatcher Black Box, 简称OSW,是提供的一个小但是非常有用的工具,它通过调用O ...

  7. [转帖]必看!PostgreSQL参数优化

    https://zhuanlan.zhihu.com/p/333201734 前不久,一个朋友所在的公司,业务人员整天都喊慢. 朋友是搞开发的,不是很懂DB,他说他们应用的其实没什么问题,但是就是每天 ...

  8. [转帖]备份VCSA内置Postgresql数据库

    首先命令行远程登录到VCSA服务器,然后执行如下命令停掉VCSA的核心服务vmware-vpxd: vCenterServerAppliance:~ # service vmware-vpxd sto ...

  9. [转帖]SPEC2006

    安装步骤 # Ubuntu16.04 # 注意安装gFortran . ./install.sh . ./shrc 一般情况下经过以上步骤即可安装完毕,进行使用,注意需要执行shrc设置完环境变量以后 ...

  10. HTTPS下tomcat与nginx的前端性能比较

    HTTPS下tomcat与nginx的前端性能比较 摘要 之前比较http的web服务器的性能. 发现nginx 比 tomcat 要好 50% 然后想到, https的情况下不知道两者有什么区别 所 ...