kotlinx-serializable @Serializable未应用于模块

h79rfbju  于 7个月前  发布在  Kotlin
关注(0)|答案(1)|浏览(232)

我在数据类上有一个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)

2exbekwf

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:

plugins {
    ...    
    kotlin("plugin.serialization") version "1.9.0" apply false
}

字符串
Module build.gradle.kts:

plugins {
    ...
    kotlin("plugin.serialization")
}
...
dependencies {
    ...
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
}

相关问题