2017-08-01 108 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

我不想忽視這個屬性。我想彌補一個缺失的財產。 –