Webflux Kotlin Coroutines Flow doesn't return any results

倖福魔咒の 提交于 2021-01-07 06:41:13

问题


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.

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.

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

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

fun findAll(): Flow<User> = dbClient.select().from(TABLE_USER_NAME).asType<User>().fetch().flow()

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

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

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

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

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

I think I should obtains some more like :

http://localhost:8080/user - HTTP 200 = {"id": 1, "email": "j@hn.doe", "signInDate": "whatever"}


回答1:


I changed my dependency

implementation("org.springframework.data:spring-data-r2dbc:BUILD-SNAPSHOT")

to

implementation("org.springframework.data:spring-data-r2dbc:1.0.0.M2")

And it works now



来源:https://stackoverflow.com/questions/56398548/webflux-kotlin-coroutines-flow-doesnt-return-any-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!