ktor网站通过freemarker自动托管';行不通
我正在与kotlin handson“Ktor互动网站”一起练习, 我没有正确的词语来解释我的代码发生了什么,但我可以说: 当我导航到http://localhost:8080/ IDE显示此错误有时不起作用ktor网站通过freemarker自动托管';行不通,freemarker,ktor,Freemarker,Ktor,我正在与kotlin handson“Ktor互动网站”一起练习, 我没有正确的词语来解释我的代码发生了什么,但我可以说: 当我导航到http://localhost:8080/ IDE显示此错误有时不起作用 2020-11-27 16:01:59.144 [main] INFO Application - No ktor.deployment.watch patterns specified, automatic reload is not active 2020-11-27 16:
2020-11-27 16:01:59.144 [main] INFO Application - No ktor.deployment.watch patterns specified, automatic reload is not active
2020-11-27 16:02:00.677 [main] INFO Application - Responding at http://0.0.0.0:8080
Exception in thread "eventLoopGroupProxy-3-1 @response-pipeline#3" kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[UnsafeBlockingTrampoline@76907f9f, Continuation at io.ktor.utils.io.jvm.javaio.OutputAdapter$loop$1.loop(Blocking.kt:311)@aa7a293]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
at kotlinx.coroutines.DispatchedTask.handleFatalException$kotlinx_coroutines_core(DispatchedTask.kt:93)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:64)
at io.ktor.utils.io.jvm.javaio.UnsafeBlockingTrampoline.dispatch(Blocking.kt:296)
at kotlinx.coroutines.DispatchedContinuation.resumeWith(DispatchedContinuation.kt:184)
at io.ktor.utils.io.jvm.javaio.BlockingAdapter$end$1.resumeWith(Blocking.kt:163)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at io.ktor.utils.io.jvm.javaio.UnsafeBlockingTrampoline.dispatch(Blocking.kt:296)
at kotlinx.coroutines.DispatchedContinuation.resumeWith(DispatchedContinuation.kt:184)
at io.ktor.utils.io.internal.CancellableReusableContinuation.resumeWith(CancellableReusableContinuation.kt:93)
at io.ktor.utils.io.ByteBufferChannel.resumeWriteOp(ByteBufferChannel.kt:2258)
at io.ktor.utils.io.ByteBufferChannel.bytesRead(ByteBufferChannel.kt:929)
at io.ktor.utils.io.ByteBufferChannel.consumed(ByteBufferChannel.kt:1953)
at io.ktor.server.netty.cio.NettyResponsePipeline$processBodyFlusher$2.invokeSuspend(NettyResponsePipeline.kt:305)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.ktor.server.netty.EventLoopGroupProxy$Companion$create$factory$1$1.run(NettyApplicationEngine.kt:215)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.ClassCastException: class kotlin.coroutines.jvm.internal.CompletedContinuation cannot be cast to class kotlinx.coroutines.DispatchedContinuation (kotlin.coroutines.jvm.internal.CompletedContinuation and kotlinx.coroutines.DispatchedContinuation are in unnamed module of loader 'app')
at kotlinx.coroutines.CoroutineDispatcher.releaseInterceptedContinuation(CoroutineDispatcher.kt:103)
at kotlin.coroutines.jvm.internal.ContinuationImpl.releaseIntercepted(ContinuationImpl.kt:118)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:39)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:55)
... 22 more
Application.kt
package com.server
import freemarker.cache.ClassTemplateLoader
import io.ktor.application.*
import io.ktor.freemarker.*
import io.ktor.http.content.*
import io.ktor.response.*
import io.ktor.routing.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
//fun main(args: Array<String>) { embeddedServer(Netty, port = 8080, module = Application::module).apply { start(wait = true) } }
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(FreeMarker) {
templateLoader = ClassTemplateLoader(this::class.java.classLoader, "templates")
}
routing {
get("/") {
call.respond(FreeMarkerContent("index.ftl",
mapOf("data" to IndexData(listOf(1, 2, 3))), ""))
}
static("/files") {
resources("files")
}
}
}
data class IndexData(val items: List<Int>)
application.conf
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ com.server.ApplicationKt.module ]
}
}
项目结构
- 服务器/主-
resources files templates Application.conf logback.xml src Application.kt
- 这是最有可能的。使用Ktor 1.5.2,您的代码可以按预期工作
resources
files
templates
Application.conf
logback.xml
src
Application.kt