com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从String反序列化类型的值:不是Enum类接受的值之一

wlzqhblo  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(206)

我正在将一个Sping Boot Web服务从Kotlin1.4.32升级到1.5.0

// Before:
kotlin("jvm") version "1.4.32"
kotlin("plugin.spring") version "1.4.32"

// After:
kotlin("jvm") version "1.5.0"
kotlin("plugin.spring") version "1.5.0"

升级并运行API后,出现以下错误:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `models.SeasonType` from String "Season": not one of the values accepted for Enum class: [NO_SEASON, UNKNOWN, SEASON]

SeasonType是一个枚举类:

package models

import com.fasterxml.jackson.annotation.JsonCreator

enum class SeasonType(val value: String) {

    SEASON("Season"),
    NO_SEASON("NoSeason"),
    UNKNOWN("Unknown");

    companion object {
        @JsonCreator
        @JvmStatic
        private fun creator(serializedValue: String): SeasonType =
            values().firstOrNull { it.value == serializedValue } ?: UNKNOWN
    }

}

为什么升级Kotlin后反序列化失败?

3htmauhk

3htmauhk1#

我能够通过升级到Kotlin1.6.x解决此问题。

相关问题