Mixin在杰克逊中添加默认单元不起作用
我想使用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
)
我想用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
:
我得到的不存在的类别是:
com.fasterxml.jackson.databind.exc.invalidtypeidexception:无法解析类型ID'foo'作为'com.amazon.amazon.ask.model.request的子类型:已知:输入IDS = [...]
I want to use Jackson mixin to provide a default implementation for an abstract type:
@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 that I want to alter with a 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 {
My objectMapper
:
However when I try to deserialize a class that is not present as subtype I get:
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'Foo' as a subtype of 'com.amazon.ask.model.Request': known type ids = [...]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使它起作用,我必须:
genericrequest
从摘要request
类延伸。当抽象类来自Java,并且继承数据类是来自Kotlin的,它会引起很多问题。
a)数据类不能从摘要类中覆盖相同的属性 https://youtrack.jetbrains.com/issue/kt-6653/kotlin-properties-do-not-not-override-java-style-getters-getters-anger-getters-and-setters
b)我必须更改
includs = as.property
toinception = jsontypeinfo.as.wrapper_arper_array
,所以我最终实现了
generiCrequest
扩展在Java ...
In order to make it work I had to:
Make
GenericRequest
extend from the abstractRequest
class.When an abstract class is from Java and inheriting data class is from Kotlin it causes lots of problems.
a) Data class cannot override same properties from abstract class https://youtrack.jetbrains.com/issue/KT-6653/Kotlin-properties-do-not-override-Java-style-getters-and-setters
b) I had to change
include = As.PROPERTY
toinclude = JsonTypeInfo.As.WRAPPER_ARRAY
So I ended up with implementing
GenericRequest
extendingRequest
in Java...