Webflux Kotlin Coroutines Flow 没有 return 任何结果
Webflux Kotlin Coroutines Flow doesn't return any results
我的 Spring 存储库为 return 一个 kotlinx.coroutines.flow.Flow 用户实现了一个功能,但似乎这个流程总是空的,即使我的数据库中有一些记录。
我正在使用 Spring Boot 2.2.0-SNAPSHOT 并支持 Kotlin 协程。我在我的存储库中创建了两种方法,一种用于创建用户,一种用于列出所有用户。创建用户的那个有效,我可以在我的数据库中看到这个用户。列出现有用户的第二个列表 return 始终是一个空列表,即使我的数据库有一些记录也是如此。
我在 Spring 应用旁边使用 PostGres 10.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()
这是 return 此处理程序编辑的:
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") }
}
}
目前我得到的唯一 return 是一个空的 json 对象:
http://localhost:8080/user - HTTP 200 = {}
我想我应该多买一些像 :
http://localhost:8080/user - HTTP 200 = {"id": 1, "email": "j@hn.doe", "signInDate": "whatever"}
我改变了我的依赖关系
implementation("org.springframework.data:spring-data-r2dbc:BUILD-SNAPSHOT")
至
implementation("org.springframework.data:spring-data-r2dbc:1.0.0.M2")
现在可以使用了
我的 Spring 存储库为 return 一个 kotlinx.coroutines.flow.Flow 用户实现了一个功能,但似乎这个流程总是空的,即使我的数据库中有一些记录。
我正在使用 Spring Boot 2.2.0-SNAPSHOT 并支持 Kotlin 协程。我在我的存储库中创建了两种方法,一种用于创建用户,一种用于列出所有用户。创建用户的那个有效,我可以在我的数据库中看到这个用户。列出现有用户的第二个列表 return 始终是一个空列表,即使我的数据库有一些记录也是如此。
我在 Spring 应用旁边使用 PostGres 10.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()
这是 return 此处理程序编辑的: 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") }
}
}
目前我得到的唯一 return 是一个空的 json 对象:
http://localhost:8080/user - HTTP 200 = {}
我想我应该多买一些像 :
http://localhost:8080/user - HTTP 200 = {"id": 1, "email": "j@hn.doe", "signInDate": "whatever"}
我改变了我的依赖关系
implementation("org.springframework.data:spring-data-r2dbc:BUILD-SNAPSHOT")
至
implementation("org.springframework.data:spring-data-r2dbc:1.0.0.M2")
现在可以使用了