我在数据类上有一个Serializable
的简单用法
@Serializable
data class Message(
val type: String,
val topics: List<String>,
val content: String
)
字符串
但IDE显示以下警告
kotlinx.serialization compiler plugin is not applied to the module, so this annotation would not be processed. Make sure that you've setup your buildscript correctly and re-import project.
型
在运行时,我收到
Serializer for class 'Message' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
型
我读过无数其他StackOverflow的帖子,其中的问题是build.gradle.kts
的设置不正确,但我三次检查我导入的插件和依赖项。
val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project
plugins {
kotlin("jvm") version "1.9.0"
kotlin("plugin.serialization") version "1.9.0"
id("io.ktor.plugin") version "2.3.2"
}
group = "com.ryandyoon"
version = "0.0.1"
application {
mainClass.set("com.ryandyoon.ApplicationKt")
val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("io.ktor:ktor-server-websockets:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
型
如果有区别的话,是的,我在一个ktor项目中使用了它,但我只是调用
Json.decodeFromString<Message>(messageString)
型
1条答案
按热度按时间2exbekwf1#
我能够解决警告“kotlinx.serialization compiler plugin is not applied to the module,so this annotation would not be processed. Make sure that you've set your buildscript correctly and re-import project.”通过在project-level build. gradle.kts中添加序列化插件,然后在module-level build. gradle. kts中应用它。
Project build.gradle.kts:
字符串
Module build.gradle.kts:
型