繁体   English   中英

如何将 kotlin 协程与响应式弹簧数据一起使用

[英]How to use kotlin coroutines with reactive spring data

我正在尝试将一些项目从 Spring Reactor 迁移到 kotlin 协程。 我有一些基于 spring webflux 的控制器,如下所示:

@RestController
class Controller(val productRepository: ProductsRepository) {

    @GetMapping("/product")
    fun find(@RequestParam id: String): Mono<Product> {
        return productRepository.findById(id)
    }
}

该控制器使用反应式弹簧数据存储库:

@Repository
interface ProductsRepository : ReactiveMongoRepository<Product, String>

根据这个官方文档- https://docs.spring.io/spring/docs/5.2.0.M1/spring-framework-reference/languages.html#how-reactive-translates-to-coroutines ,我的功能find的控制器应该被转换为suspend fun并且这个函数应该返回一个 Product 类的实例,而不是 Product 的反应式 Mono 包装器。 类似的东西:

@RestController
class Controller(val productRepository: ProductsRepository) {

    @GetMapping("/product")
    suspend fun find(@RequestParam id: String): Product {
        return productRepository.findById(id)
    }
}

但是我的 productRepository 处理 Mono 和 Flux,而不是挂起的函数。 在这种情况下,我应该如何正确使用 spring 数据抽象?

这可以通过有用的kotlinx-coroutines-reactor辅助库来实现,该库为项目反应器Publisher提供有用的扩展方法,以帮助将MonoFlux转换为 kotlin 协程。

首先添加一个依赖

 <dependency>
     <groupId>org.jetbrains.kotlinx</groupId>
     <artifactId>kotlinx-coroutines-reactor</artifactId>
 </dependency>

(如果您使用 spring-boot,则不必指定版本,因为它会为您管理它)

您现在可以使用kotlinx.coroutines.reactive.awaitFirstOrNullMono<Product>转换为Product? 并“等待”结果。

@RestController
class Controller(val productRepository: ProductsRepository) {

    @GetMapping("/product")
    suspend fun find(@RequestParam id: String): Product? {
        return productRepository.findById(id).awaitFirstOrNull()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM