gpt4 book ai didi

java - 使用接口(interface)反序列化 Kotlin 类时出现 InvalidDefinitionException

转载 作者:行者123 更新时间:2023-12-02 12:57:58 31 4
gpt4 key购买 nike

我有几个 Kotlin 数据类实现了这个 TypeAlias 接口(interface):

import com.fasterxml.jackson.annotation.JsonValue;

public interface TypeAlias<T> {
@JsonValue
T getValue();
}

以前,我一直在使用早期版本的 Jackson,它们反序列化得很好。但现在我使用的是 2.9.9。

一个无法反序列化的类:
data class UserId(@NotNull private val value: EntityId) : TypeAlias<EntityId> {
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
constructor(value: String) : this(EntityId(value))

override fun getValue() = value
}

测试它:
@Test
fun `Deserialization test`() {
val userIdString = "\"aaaaaaaaaaaaaaaaaaaaaa\""
mapper.readValue(userIdString, UserId::class.java)
}

这个测试爆炸了:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `UserId` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('aaaaaaaaaaaaaaaaaaaaaa')

我的映射器确实注册了 KotlinModule。

请注意,UserId 在其辅助 String 构造函数上确实有一个 JsonCreator。这种行为让我认为界面导致 jackson 看不到@JsonCreator 注释。我在这里想念什么?

最佳答案

在与几个同事深入研究后,我们意识到问题与在 ObjectMapper 上注册模块的顺序有关。我们在篡改配置的 Kotlin 模块之后注册了一个模块。当我们在有问题的模块之后注册 Kotlin 模块时,一切都恢复了。

关于java - 使用接口(interface)反序列化 Kotlin 类时出现 InvalidDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57061435/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com