Webflux Kotlin Coroutines Flow不返回任何结果 [英] Webflux Kotlin Coroutines Flow doesn't return any results

查看:86
本文介绍了Webflux Kotlin Coroutines Flow不返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Spring存储库实现了一个函数来返回User的kotlinx.coroutines.flow.Flow,但即使我的数据库中有一些记录,该流也总是空的.

My Spring repository implement a function to return a kotlinx.coroutines.flow.Flow of User but it seems this flow is always empty even if there are some record in my DB.

我正在使用带有Kotlin协程支持的Spring Boot 2.2.0-SNAPSHOT.我在存储库中创建了两种方法,一种用于创建用户,另一种用于列出所有用户.创建用户的工作正常,我可以在数据库中看到该用户.列出现有用户的第二个列表始终返回一个空列表,即使我的数据库中有一些记录也是如此.

I am using Spring Boot 2.2.0-SNAPSHOT with the Kotlin coroutines support. I created two methods in my repository, one to create an user and one to list all users. The one to create an user works and I can see this user in my DB. The second one to list existing users returns an empty list, always, even if my DB has some records.

我在Spring应用程序旁边使用一个PostGres 10.1 Docker实例.

I am using a PostGres 10.1 docker instance next to my Spring app.

完整项目可在github上找到: https://github.com/kizux/demo-spring-webflux-kotlin

The full project is available on github : https://github.com/kizux/demo-spring-webflux-kotlin

这是我的存储库的方法实现:

Here is my repository's method implementation :

src/main/kotlin/fr/kizux/kotlindemocoroutines/repository/UserRepository.kt

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

This is returned by this handler : 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

And routed at : 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

I also tried to add a log at the startup of my app : 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对象:

Currently the only return I got is an empty json object :

http://localhost:8080/user -HTTP 200 = {}

http://localhost:8080/user - HTTP 200 = {}

我认为我应该获得更多类似的东西:

I think I should obtains some more like :

http://localhost:8080/user -HTTP 200 = {"id":1,电子邮件":"j@hn.doe","signInDate":无论如何"}

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")

现在就可以使用

这篇关于Webflux Kotlin Coroutines Flow不返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆