当前位置: 首页 > news >正文

Spring WebFlux学习笔记(一)

核心思想

WebFlux主要是异步

例子

参考一个源码:
https://blog.csdn.net/qq_43923045/article/details/106309432?spm=1001.2014.3001.5506

@GetMapping("/delay1")public Mono<RestResult> delayResult() {long l = System.currentTimeMillis();Mono<RestResult> resultMono = Mono.fromSupplier(()->{try {Thread.sleep(5_000);} catch (InterruptedException e) {e.printStackTrace();}return new RestResult().setMessage("hello world!!");});long r = System.currentTimeMillis();log.info("执行时间 {} !!",(r-l));return resultMono;}@GetMapping("/delay2")public  RestResult  delayResult2() {long l = System.currentTimeMillis();Supplier<RestResult> result = () -> {try {Thread.sleep(5_000);} catch (InterruptedException e) {e.printStackTrace();}return new RestResult<>().setMessage("ok");};RestResult restResult = result.get();long r = System.currentTimeMillis();log.info("执行时间 {} !!",(r-l));return restResult;}

如上面的代码例子,delay1接口会直接打印执行时间。delay2会等待一段时间才能打印执行时间。

扩展

思考:
对于delay1接口,什么时候才有返回结果呢?
答案:
返回需要加上MediaType.APPLICATION_STREAM_JSON_VALUE

@GetMapping(value = "/stream",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)

参考资料https://blog.csdn.net/weixin_34364071/article/details/91664821?spm=1001.2014.3001.5506

遗留问题

1、那使用postman等工具测试例子的2个接口的时间到底是多少?
2、前端怎么去处理异步返回的结果?

http://www.lryc.cn/news/473232.html

相关文章:

  • 富格林:正确追损思维安全交易
  • 前端vue2迁移至uni-app
  • 恋爱脑学Rust之闭包三Traits:Fn,FnOnce,FnMut
  • 区块链介绍
  • git回滚间隔的提交
  • Map和Set(数据结构)
  • vue3uniapp实现自定义拱形底部导航栏,解决首次闪烁问题
  • 新需求编码如何注意低级错误代码
  • 系统架构图设计(行业领域架构)
  • windows 文件监控 c++ 11及以上版本可用
  • jsMind:炸裂项目,用JavaScript构建的思维导图库,GitHub上的热门开源项目
  • postman的脚本设置接口关联
  • 【python】OpenCV—Tracking(10.3)—GOTURN
  • git pull遇到一个问题
  • 书生-第四期闯关:完成SSH连接与端口映射并运行hello_world.py
  • 【CSS3】css开篇基础(5)
  • AI产品独立开发变现实战营,炒掉老板做自由职业赚大钱
  • 【UE5.3 Cesium for Unreal】编译GlobePawn
  • idea连接数据库出现错误的解决方式
  • 数据分级分类工具:敏感数据识别中的AI智能化转型之路
  • 乘云而上,OceanBase再越山峰
  • 设计模式4-工厂模式策略模式
  • 使用Html5基本标签实现“时空电影网”案例步骤及详细代码
  • Servlet 3.0 新特性全解
  • VUE组件学习 | 五、v-for组件
  • uniapp写移动端,适配苹果手机底部导航栏,ios安全区问题,苹果手机遮挡底部信息,uview的u-action-sheet组件
  • CentOS9 Stream上安装Edge浏览器
  • el-datepicker此刻按钮点击失效
  • VUE组件学习 | 六、v-if, v-else-if, v-else组件
  • 机器学习算法之回归算法