首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ktor快速启动HTTP API -错误应用程序-未处理: GET - /snippets

Ktor快速启动HTTP API -错误应用程序-未处理: GET - /snippets
EN

Stack Overflow用户
提问于 2018-11-22 19:37:00
回答 1查看 1.4K关注 0票数 2

我是Ktor新手,目前正在使用quick start http api,但收到以下错误:

错误应用程序-未处理:无法将GET - /snippets com.fasterxml.jackson.databind.JsonMappingException: kotlin.reflect.jvm.internal.KClassImpl强制转换为kotlin.reflect.jvm.internal.KClassImpl (通过引用链: java.util.Collections$Singleton映射“snippets”->java.util.ArrayList)

代码:

代码语言:javascript
复制
import com.fasterxml.jackson.databind.SerializationFeature
import io.ktor.application.*
import io.ktor.features.CallLogging
import io.ktor.features.ContentNegotiation
import io.ktor.features.DefaultHeaders
import io.ktor.jackson.jackson
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.Routing
import io.ktor.routing.get
import io.ktor.routing.post
import io.ktor.routing.routing
import java.util.*

data class Snippet(val text: String)

val snippets = Collections.synchronizedList(mutableListOf(
    Snippet("hello"),
    Snippet("world")
))

fun Application.main() {
    install(ContentNegotiation) {
        jackson {
            enable(SerializationFeature.INDENT_OUTPUT)
        }
    }
    routing {
        get("/snippets") {
            call.respond(mapOf("snippets" to synchronized(snippets) { snippets.toList() }))
        }
    }
}

如果我使用下面的代码:

代码语言:javascript
复制
 call.respond(mapOf("snippets" to synchronized(snippets) { snippets.toString() }))

它返回:

代码语言:javascript
复制
   {
  "snippets" : "[Snippet(text=hello), Snippet(text=world)]"
   }

但是现在它使用的是toString()而不是toList(),你知道如何让它像使用toList()快速入门那样工作吗?

EN

回答 1

Stack Overflow用户

发布于 2018-11-22 23:07:58

找到问题所在。

使用application.conf文件中的watch选项来运行服务器似乎会把一些事情搞砸。

application.conf文件:

代码语言:javascript
复制
ktor {
    deployment {
        port = 8080
        watch = [ / ]
    }

    application {
        modules = [ com.MainKt.main ]
    }
}

删除

代码语言:javascript
复制
      watch = [ / ]

或者切换回嵌入式服务器似乎已经解决了这个问题。

代码语言:javascript
复制
fun main() {
    embeddedServer(Netty, 8080) {

         //rest of the code

    }.start(wait = true)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53430129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档