我的Spring存储库实现了一个函数来返回一个用户的kotlinx.coroutines.flow.Flow,但是这个流似乎总是空的,即使我的数据库中有一些记录。
我使用带有Kotlin协程支持的Spring Boot 2.2.0-SNAPSHOT。我在存储库中创建了两个方法,一个用于创建用户,另一个用于列出所有用户。创建一个用户的工作,我可以在我的数据库中看到这个用户。第二个列出现有用户的命令总是返回一个空列表,即使我的数据库有一些记录也是如此。
我在我的PostGres应用程序旁边使用了Spring10.1 docker实例。
完整的项目可以在github上找到:https://github.com/kizux/demo-spring-webflux-kotlin
下面是我的存储库的方法实现:
src/main/kotlin/fr/kizux/kotlindemocoroutines/repository/UserRepository.kt
fun findAll(): Flow<User> = dbClient.select().from(TABLE_USER_NAME).asType<User>().fetch().flow()
这由这个处理程序返回: src/main/kotlin/fr/kizux/kotlindemocoroutines/handler/UserHandler.kt
suspend fun getAll(req: ServerRequest): ServerResponse = ServerResponse.ok().bodyAndAwait(userRepo.findAll())
并在src/main/kotlin/fr/kizux/kotlindemocoroutines/configuration/RouterConfig.kt中路由
@Bean
fun userRoutes(userHandler: UserHandler) = coRouter {
"/user".nest {
GET("", userHandler::getAll)
POST("", userHandler::create)
}
}
我还尝试在我的应用程序启动时添加一个日志: src/main/kotlin/fr/kizux/kotlindemocoroutines/KotlinDemoCoroutinesApplication.kt
@EventListener(value = [ApplicationReadyEvent::class])
fun init() {
runBlocking {
userRepo.save(User(email="j@hn.doe", signInDate=LocalDateTime.now()))
userRepo.findAll().onEach { user -> println("Here is $user") }
}
}
目前我得到的唯一返回是一个空的json对象:
http://localhost:8080/user -HTTP200= {}
我想我应该得到更多这样的东西:
http://localhost:8080/user -HTTP200= {"id":1,"email":"j@hn.doe","signInDate":“随便”}
发布于 2019-06-03 00:58:31
我改变了我的依赖
implementation("org.springframework.data:spring-data-r2dbc:BUILD-SNAPSHOT")
至
implementation("org.springframework.data:spring-data-r2dbc:1.0.0.M2")
现在它起作用了
https://stackoverflow.com/questions/56398548
复制相似问题