【发布时间】:2020-01-17 16:40:42
【问题描述】:
在使用 Jackson 反序列化 LocationGeneric 时,我在 Kotlin 中遇到以下问题。当我没有向用于构造具体类的抽象类添加任何额外信息时,就会出现这种情况。当我反序列化 LocationOne 或 LocationTwo 时效果很好。
这是我写的代码:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY,
property = "type", visible = true)
@JsonSubTypes(
JsonSubTypes.Type(value = LocationOne::class, name = "ONE"),
JsonSubTypes.Type(value = LocationTwo::class, name = "TWO"),
JsonSubTypes.Type(value = LocationGeneric::class, name = "GENERIC_1"),
JsonSubTypes.Type(value = LocationGeneric::class, name = "GENERIC_2")
)
abstract class Location(
val type: String
)
class LocationGeneric(
type: String
): Location(type)
class LocationOne(
type: String,
val somethingSpecific: String
): Location(type)
class LocationAirport(
type: String,
val somethingElse: String
): Location(type)
这是我得到的错误:
无法构造
Location的实例(尽管至少有一个 Creator 存在):不能从对象值反序列化(没有委托-或 基于属性的创建者)
我尝试将抽象类更改为开放类,但到目前为止没有运气。我为其他情况工作。为什么在LocationGeneric 案例中找不到默认的 Kotlin 构造函数?有什么想法吗?
【问题讨论】:
标签: kotlin jackson deserialization