spring - Kotlin 协程和 Spring Framework 5 react 类型

标签 spring kotlin coroutine spring-webflux kotlinx.coroutines

Kotlin 协程允许通过返回 Deferred 值来执行非阻塞代码。这对于在使用阻塞方法(例如来自库)时生成非阻塞代码非常有用。

Spring 5 允许在框架中使用 MonoFlux。我看到的最大兴趣是能够序列化这两种类型的实例,并在有人调用 Controller 端点时将其作为响应发送回。

Spring 5 的一大亮点是对 Kotlin 的特定支持(路由器、bean 声明等),但我找不到有关 Kotlin 协程和 Spring 5 react 类型之间可能交互的信息。

有什么方法可以结合这些功能的优点吗?

  • Deferred 转换为 Mono/Flux
  • 有一个 Deferred 作为 Spring Controller 方法的响应类型?

如果没有,如果我们有 Spring 5 react 类型,在哪些情况下协程有意义?

最佳答案

截至 5.2 版(仍在开发中),Spring Framework 提供了对协程的官方支持。我写了a detailed blog post这解释了 MonoFlux 类型如何映射到挂起函数、Deferred 和 Kotlin Flow 类型。您还可以在 coroutines section of Spring Framework 5.2 reference documentation 中找到更多详细信息.

关于spring - Kotlin 协程和 Spring Framework 5 react 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689682/

相关文章:

java - 获取 org.dom4j.DocumentException : feature read only while deploying war file in tomcat of linux 异常

java - Spring Boot Admin 日志中重复出现 AsyncRequestTimeoutException

c++ - 使用 boost::asio stackless 协程通过 HTTP 下载多个文件

spring - Hibernate 与 Spring 的集成在 hippo cms 站点项目中不起作用

java - Spring - 使用 HttpEntity 从 ResponseEntity 获取正文的通用方法

android - 在 Recyclerview 中错误地运行 onBindViewHolder - Kotlin

xml - 使用 Kotlin 解析 XML?

java - 没有 IDE 帮助的 Gradle 的 Kotlin 设置 - Kotlin 类未进入类路径

asynchronous - Kotlin协程中的launch/join和async/await有什么区别

c++ - 分段堆栈是否可以与其他库自由使用