最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Fasterxml Jackson polymorphic deserialization with WRAPPER_OBJECT: Could not resolve type id 'field' as a

programmeradmin1浏览0评论

It seems that fasterxml treats the property of the parent object incorrectly, when used with JsonTypeInfo.As.WRAPPER_OBJECT. We have the following hierarchy:

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.WRAPPER_OBJECT
)
@JsonSubTypes({
        @JsonSubTypes.Type(value = FailedNotification.class, name = "FailedNotification"),
        @JsonSubTypes.Type(value = StartedNotification.class, name = "StartedNotification")
})
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Notification {
    @NotNull
    private String code;
}

And subtypes:

@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
@JsonRootName("FailedNotification")
public class FailedNotification extends Notification {

    private String sourceId;
}
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
@JsonRootName("StartedNotification")
public class StartedNotification extends Notification {

    private String sourceId;
}

But the deserialization

        new XmlMapper().readValue("""
                <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
                <StartedNotification>
                  <code>123</code>
                  <sourceId>source-is-here</sourceId>
                </StartedNotification>
                """, Notification.class);

fails with:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'code' as a subtype of Notification: known type ids = [FailedNotification, Notification, StartedNotification]

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论