admin管理员组

文章数量:1122846

I have a class JobRequest

public class JobRequest {

    private String type;
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
    @JsonSubTypes({
            @JsonSubTypes.Type(value = AJobDetails.class, name = "A"),
            @JsonSubTypes.Type(value = BJobDetails.class, name = "B")
    })
    private JobDetails job;
}

The issue is the key 'job' might not be present or null in the recieved JSON. When the model is initialised it throws an error.

MismatchedInputException: Missing property 'job' for external type id 'type' Tried JsonInclude, defaultImpl=Void.class, and Optional.

In case of Optional, error is gone, but populated values are also populated as null.

本文标签: How to allow nullable fields along with JsonTypeInfo in Spring Boot and JacksonStack Overflow