com.fasterxml.jackson.databind.exc.invalidformatexception:无法从字符串中估算类型的值:不是enum class接受的值之一

发布于 2025-01-19 13:29:33 字数 1107 浏览 0 评论 0原文

我正在将Spring Boot Web服务从Kotlin 1.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]

sepentype是枚举类别:

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后,当挑战失败?

I'm upgrading a Spring Boot web service from Kotlin 1.4.32 to 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"

After upgrading and running the API, I get the following error:

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 is an enum class:

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
    }

}

Why does deserialization fail after upgrading Kotlin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡莣 2025-01-26 13:29:33

我通过升级到 Kotlin 1.6.x 解决了这个问题。

I was able to resolve this issue by upgrading to Kotlin 1.6.x.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文