在 ktor 插件“序列化”中,不能与“FreeMaker”插件一起使用

has*_*aty 4 ktor

好的,我是 Ktor 的新手,我正在学习它,在此过程中我遇到的一件事是这个错误:kotlinx.serialization.SerializationException:未找到类“FreeMarkerContent”的序列化器。将类标记为 @Serializable 或显式提供序列化器

那是因为在我的代码中,我使用 FreeMakcerContent 并尝试对其进行序列化:

 get {
        call.respond(FreeMarkerContent("index.ftl", mapOf("articles" to articles)))
    }
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Moh*_*wia 7

我遇到了同样的问题,我通过在安装插件之前FreeMarker安装插件来解决它。ContentNegotiation

install(FreeMarker) {
        templateLoader = ClassTemplateLoader(this::class.java.classLoader, "templates")
}
install(ContentNegotiation) {
        json()
}
routing {
        get("/html-freemarker") {
            call.respond(FreeMarkerContent("index.ftl", mapOf("name" to "Shalaga"), ""))
        }
}
Run Code Online (Sandbox Code Playgroud)