一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

  • 搜索系列

  • MQ系列

  • WEB系列

    • Request

    • Response

    • RestTemplate

    • WebClient

      • 【WEB系列】WebClient之基础使用姿势
      • 【WEB系列】WebClient之文件上传
      • 【WEB系列】WebClient之请求头设置
      • 【WEB系列】WebClient之Basic Auth授权
      • 【WEB系列】WebClient之超时设置
      • 【WEB系列】WebClient之retrieve与exchange的使用区别介绍
      • 【WEB系列】WebClient之非200状态码信息捕获
      • 【WEB系列】WebClient之策略设置
      • 【WEB系列】WebClient之同步与异步
        • I. 项目环境
          • 1. 依赖
          • 2. 测试接口
        • II. 同步返回
          • 1. 实现方式
        • III. 其他
          • 0. 项目
          • 1. 一灰灰Blog
    • WebFlux

    • WebSocket

    • Web三剑客

    • 实例

    • 其他

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • WEB系列
  • WebClient
一灰灰
2020-08-20

【WEB系列】WebClient之同步与异步

回顾一下最开始介绍WebClient的使用姿势之前,我们介绍了AsyncRestTemplate来实现异步的网络请求;但是在Spring5之后,官方推荐使用WebClient来替换AsyncRestTemplate实现异步请求;所以一般来讲,WebClient适用于异步的网络访问,但是,假设我需要同步获取返回结果,可行么?

# I. 项目环境

本项目借助SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA进行开发

# 1. 依赖

使用WebClient,最主要的引入依赖如下(省略掉了SpringBoot的相关依赖,如对于如何创建SpringBoot项目不太清楚的小伙伴,可以关注一下我之前的博文)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
1
2
3
4

# 2. 测试接口

@GetMapping(path = "get")
public Mono<String> get(String name, Integer age) {
    return Mono.just("req: " + name + " age: " + age);
}

@GetMapping(path = "mget")
public Flux<String> mget(String name, Integer age) {
    return Flux.fromArray(new String[]{"req name: " + name, "req age: " + age});
}
1
2
3
4
5
6
7
8
9

# II. 同步返回

需要同步返回结果还是比较简单的,获取对应的Mono/Flux之后调用一下block()方法即可,但是需要注意,这里也有一个潜在的坑

# 1. 实现方式

public void sync() {
    // 同步调用的姿势

    // 需要特别注意,这种是使用姿势,不能在响应一个http请求的线程中执行;
    // 比如这个项目中,可以通过  http://127.0.0.1:8080/test 来调用本类的测试方法;但本方法如果被这种姿势调用,则会抛异常;
    // 如果需要正常测试,可以看一下test下的调用case

    WebClient webClient = WebClient.create("http://127.0.0.1:8080");

    String ans = webClient.get().uri("/get?name=一灰灰").retrieve().bodyToMono(String.class).block();
    System.out.println("block get Mono res: " + ans);


    Map<String, Object> uriVariables = new HashMap<>(4);
    uriVariables.put("p1", "一灰灰");
    uriVariables.put("p2", 19);

    List<String> fans =
            webClient.get().uri("/mget?name={p1}&age={p2}", uriVariables).retrieve().bodyToFlux(String.class)
                    .collectList().block();
    System.out.println("block get Flux res: " + fans);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

项目启动之后,我们写一个测试类来调用这个方法

@Test
public void sync() {
    WebClientTutorial web = new WebClientTutorial();
    web.sync();
}
1
2
3
4
5

如果我们换成下面这种写法,就会报错了

@GetMapping(path = "test")
public String test() {
    WebClientTutorial web = new WebClientTutorial();
    web.sync();
    return "over";
}
1
2
3
4
5
6

# III. 其他

# 0. 项目

系列博文

  • 【WEB系列】WebClient之策略设置 (opens new window)
  • 【WEB系列】WebClient之非200状态码信息捕获 (opens new window)
  • 【WEB系列】WebClient之retrieve与exchange的使用区别介绍 (opens new window)
  • 【WEB系列】WebClient之超时设置 (opens new window)
  • 【WEB系列】WebClient之Basic Auth授权 (opens new window)
  • 【WEB系列】WebClient之请求头设置 (opens new window)
  • 【WEB系列】WebClient之文件上传 (opens new window)
  • 【WEB系列】WebClient之基础使用姿势 (opens new window)

源码

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 源码:https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/222-web-client (opens new window)

# 1. 一灰灰Blog

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

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#WebClient
上次更新: 2021/10/15, 19:56:22
【WEB系列】WebClient之策略设置
【WEB系列】WebFlux之初体验

← 【WEB系列】WebClient之策略设置 【WEB系列】WebFlux之初体验→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×