Mixin在杰克逊中添加默认单元不起作用

发布于 2025-02-02 21:22:28 字数 1170 浏览 5 评论 0原文

我想使用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:

object mapper

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 技术交流群。

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

发布评论

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

评论(1

情深已缘浅 2025-02-09 21:22:28

为了使它起作用,我必须:

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 to inception = jsontypeinfo.as.wrapper_arper_array

所以我最终实现了generiCrequest扩展在Java ...

In order to make it work I had to:

Make GenericRequest extend from the abstract Request 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 to include = JsonTypeInfo.As.WRAPPER_ARRAY

So I ended up with implementing GenericRequest extending Request in Java...

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