当我尝试使用 ktor 和 moshi 在服务器端解码 json 时,我得到 "kotlinx/coroutines/io/ByteReadChannel"

I get "kotlinx/coroutines/io/ByteReadChannel" when I try to decode a json on server side with ktor and moshi

在 Application.kt 我在 Application.module

安装 moshi
   install(ContentNegotiation){
        moshi()
    }

我声明了一个简单的测试 calss 并在路径中尝试解码测试 class:

data class Test(val testString: String)

fun Route.test() {
    post (TEST_ENDPOINT) {
        val testReceive = call.receive<Test>()
        call.respond(testReceive)
    }

请求是 post,具有以下 Headers 和 Body:

Accept: */*
Accept-Encoding: gzip, deflate
Content-Type: application/json
Accept-Language: en-gb

{
  "testString": "dasdada"
}

回复headers:

HTTP/1.1 500 Internal Server Error

Date: Mon, 05 Oct 2020 11:55:58 GMT
Content-Length: 37
Connection: keep-alive
Content-Type: text/plain; charset=UTF-8
Server: ktor-server-core/1.4.1 ktor-server-core/1.4.1

回复body:

kotlinx/coroutines/io/ByteReadChannel

如有任何建议或意见,我们将不胜感激。

Moshi 1.0.1 依赖于一些过时的 Ktor API。 考虑回到 Ktor 1.3.2 或(更好)使用另一个 JSON 处理程序(有几个开箱即用的处理程序:Gson, Jackson and kotlinx.serialization