在正式的Kotlin教程https://kotlinlang.org/docs/multiplatform-full-stack-app.html中
我运行了一个ktor服务器,当我执行GET时,我得到了正确的json响应。
POST http://localhost:9090/shoppingList
Content-Type: application/json
{
"desc": "Peppers 🌶",
"priority": 5
}
服务器返回500并显示以下错误:
2023-01-10 08:53:06.605 [DefaultDispatcher-worker-1] INFO ktor.application - Responding at http://0.0.0.0:9090
2023-01-10 08:53:12.317 [eventLoopGroupProxy-4-2] ERROR ktor.application - Unhandled: POST - /shoppingList
java.lang.VerifyError: class kotlinx.serialization.json.internal.StreamingJsonDecoder overrides final method kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(Lkotlinx/serialization/descriptors/SerialDescriptor;ILkotlinx/serialization/DeserializationStrategy;Ljava/lang/Object;)Ljava/lang/Object;
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at ...
这只是在Kotlin中构建全栈Web应用程序的教程的第一部分,所以我想在教程之外工作,否则我会错过一些东西。
下面的服务器代码,但这也是复制和粘贴的权利,并使用教程git repo
val shoppingList = mutableListOf(
ShoppingListItem("Cucumbers 🥒", 1),
ShoppingListItem("Tomatoes 🍅", 2),
ShoppingListItem("Orange Juice 🍊", 3)
)
fun main() {
embeddedServer(Netty, 9090) {
install(ContentNegotiation) {
json()
}
install(CORS) {
allowMethod(HttpMethod.Get)
allowMethod(HttpMethod.Post)
allowMethod(HttpMethod.Delete)
anyHost()
}
install(Compression) {
gzip()
}
routing {
route(ShoppingListItem.path) {
get {
call.respond(shoppingList)
}
post {
shoppingList += call.receive<ShoppingListItem>()
call.respond(HttpStatusCode.OK)
}
delete("/{id}") {
val id = call.parameters["id"]?.toInt() ?: error("Invalid delete request")
shoppingList.removeIf { it.id == id }
call.respond(HttpStatusCode.OK)
}
}
}
routing {
get("/hello") {
call.respondText("Hello, API!")
}
}
}.start(wait = true)
1条答案
按热度按时间7vux5j2d1#
这是因为教程repo的主分支已过期,需要根据此问题更新其依赖项:https://github.com/kotlin-hands-on/jvm-js-fullstack/issues/21