Mixin 在 Jackson 中添加 defaultImpl 不起作用
Mixin adding defaultImpl in Jackson does not work
我想使用 Jackson mixin 为抽象类型提供默认实现:
@JsonTypeInfo(
use = Id.NAME,
include = As.PROPERTY,
property = "type",
visible = true,
defaultImpl = GenericRequest::class
)
@JsonMixin(Request::class)
class AlexaRequestMixin {
}
data class GenericRequest(
val type: String, val requestId: String, val timestamp: OffsetDateTime
)
Base class 我想用 mixin 改变:
@JsonTypeInfo(
use = Id.NAME,
include = As.PROPERTY,
property = "type",
visible = true
)
@JsonSubTypes({@Type(
value = InstallationError.class,
name = "Alexa.DataStore.PackageManager.InstallationError"
),
// ...
)})
public abstract class Request {
我的objectMapper
:
然而,当我尝试反序列化不作为子类型存在的 class 时,我得到:
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'Foo' as a subtype of 'com.amazon.ask.model.Request': known type ids = [...]
为了让它发挥作用,我必须:
使 GenericRequest
从抽象 Request
扩展 class。
当抽象 class 来自 Java 并且继承数据 class 来自 Kotlin 时,会导致很多问题。
a) 数据 class 无法覆盖抽象 class https://youtrack.jetbrains.com/issue/KT-6653/Kotlin-properties-do-not-override-Java-style-getters-and-setters
中的相同属性
b) 我不得不将 include = As.PROPERTY
更改为 include = JsonTypeInfo.As.WRAPPER_ARRAY
所以我最终实现了 GenericRequest
在 Java 中扩展 Request
...
我想使用 Jackson mixin 为抽象类型提供默认实现:
@JsonTypeInfo(
use = Id.NAME,
include = As.PROPERTY,
property = "type",
visible = true,
defaultImpl = GenericRequest::class
)
@JsonMixin(Request::class)
class AlexaRequestMixin {
}
data class GenericRequest(
val type: String, val requestId: String, val timestamp: OffsetDateTime
)
Base class 我想用 mixin 改变:
@JsonTypeInfo(
use = Id.NAME,
include = As.PROPERTY,
property = "type",
visible = true
)
@JsonSubTypes({@Type(
value = InstallationError.class,
name = "Alexa.DataStore.PackageManager.InstallationError"
),
// ...
)})
public abstract class Request {
我的objectMapper
:
然而,当我尝试反序列化不作为子类型存在的 class 时,我得到:
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'Foo' as a subtype of 'com.amazon.ask.model.Request': known type ids = [...]
为了让它发挥作用,我必须:
使 GenericRequest
从抽象 Request
扩展 class。
当抽象 class 来自 Java 并且继承数据 class 来自 Kotlin 时,会导致很多问题。
a) 数据 class 无法覆盖抽象 class https://youtrack.jetbrains.com/issue/KT-6653/Kotlin-properties-do-not-override-Java-style-getters-and-setters
中的相同属性b) 我不得不将 include = As.PROPERTY
更改为 include = JsonTypeInfo.As.WRAPPER_ARRAY
所以我最终实现了 GenericRequest
在 Java 中扩展 Request
...