2017-08-01 35 views
2

我有这个类的外部属性 “的contentType”:杰克逊2.5 - JsonMappingException:缺少外部类型id属性

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: Missing external type id property 'contentType'

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

我想要的是在'contentType'字段丢失的情况下,使用默认值。

在此先感谢。

回答

0

您可以使用@JsonIgnoreProperties(ignoreUnknown=true)对类进行注释。

+1

我不想忽视这个属性。我想弥补一个缺失的财产。 –