【问题标题】:Jackson 2.5 - JsonMappingException: Missing external type id propertyJackson 2.5 - JsonMappingException:缺少外部类型 id 属性
【发布时间】:2023-04-02 18:45:01
【问题描述】:

我有一个带有外部属性“contentType”的类:

public class ContentEvent implements AbstractMessagingEvent{
    @Valid
    @JsonTypeInfo(include = JsonTypeInfo.As.EXTERNAL_PROPERTY, use = NAME, property = "contentType")
    public final ContentValue message;

    public ContentEvent(ContentValue message) {
        this.message = message;
    }

    public ContentEvent() {
        this(null);
    }

    public static ContentEvent example () {
        return new ContentEvent(HostedFile.example());
    }
}

“contentType”可以是以下之一:

@JsonSubTypes({
        @JsonSubTypes.Type(SecureFormSubmission.class),
        @JsonSubTypes.Type(SecureFormInvitation.class),
        @JsonSubTypes.Type(TextPlain.class),
        @JsonSubTypes.Type(HostedFile.class),
        @JsonSubTypes.Type(ExternalFile.class)
})
public interface ContentValue{
}

当我尝试反序列化缺少“contentType”字段的 JSON 时,我收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException:缺少外部类型 id 属性“contentType”

我尝试添加一个 'defaultImpl=NoClass.class' 和一个 defaultImpl=MyOwnCustomClass' 并清除错误,但结果是一个没有任何 'contentType' 的对象。

我想要的是在缺少“contentType”字段的情况下使用默认值。

提前致谢。

【问题讨论】:

    标签: java json serialization jackson


    【解决方案1】:

    你可以用@JsonIgnoreProperties(ignoreUnknown=true)注释类。

    【讨论】:

    • 我不想忽略这个属性。我想弥补缺失的财产。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2019-09-11
    • 2018-08-27
    • 1970-01-01
    • 2017-01-06
    相关资源
    最近更新 更多