Warm tip: This article is reproduced from serverfault.com, please click

Ktor

发布于 2020-11-18 23:57:14

I a new to Kotlin and Ktor in particular, so I have tried to do simple post request. As you can see below, there is nothing special.

    routing {
    post("/articles/add"){
        val post = call.receive<ArticleRequest>()
        println(post)
    }

Error shown in logs is below and I don't understand why I should use here coroutines.

ERROR Application - Unhandled: POST - /articles/add 
java.lang.IllegalStateException: Using blocking primitives on this dispatcher is not allowed. Consider using async channel instead or use blocking primitives in withContext(Dispatchers.IO) instead.

I am using 1.4.2 version. I would appreciate any help.

Questioner
bwnuk
Viewed
0
shadowsheep 2020-11-19 20:58:55

If you are using Jackson this is a bug and there is a suggested workaround:

routing {
    post("/articles/add") {
        with(Dispatchers.IO) {
            val post = call.receive<ArticleRequest>()
            println(post)
        }
    }
}

Or you can rollback to 1.4.1 until the bug is solved.