Kizux
Kizux

Reputation: 105

Webflux Kotlin Coroutines Flow doesn't return any results

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="[email protected]", 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": "[email protected]", "signInDate": "whatever"}

Upvotes: 1

Views: 829

Answers (1)

Kizux
Kizux

Reputation: 105

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

Upvotes: 1

Related Questions