简体   繁体   中英

Jackson-databind deserializes JSON with missing property as "null" instead of default value

When deserializing a JSON string, missing properties are being set as "null" when they shouldn't be. Below is a POJO class:

    @Builder
    @Getter
    @Setter
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    @EqualsAndHashCode
    static class MyPojo {
        
        @JsonProperty(value = "OptionalProp", required = false, defaultValue = "")
        @Builder.Default
        @Nullable
        @JsonSetter(value = "", nulls = Nulls.AS_EMPTY)
        private String optionalProp = "";
    
        @JsonProperty(value = "RequiredProp", required = false, defaultValue = "")
        @Builder.Default
        @Nullable
        @JsonSetter(value = "", nulls = Nulls.AS_EMPTY)
        private String requiredProp = "";
    }

JSON String to deserialize:

{
  "RequiredProp" : "test"
}

Here is the deserialization:

private final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final myPojo = OBJECT_MAPPER.readValue(inputStream, MyPojo.class);

And here is the output:

MyPojo(optionalProp=null, requiredProp=test)

BUT creating the POJO with builder:

        final MyPojo myPojo = MyPojo.builder()
            .requiredProp("test")
            .build();

Results in the following POJO:

MyPojo(optionalProp=, requiredProp=test)

I'm using:

Jackson-databind 2.12.x
Jackson-annotation 2.12.x
Jackson-core 2.12.x

Is there a minor version change from one of these packages that changes the behavior?

The problem stems from Lombok and not the actual Jackson stuff. Taking a look at the generated source code will give you a clear view of what's happening. Using Builder.Default seems to cause the issue as it apparently produces messed up code which removes the initializations you have in place. Removing the annotation restores proper behavior.

This seems to be an issue with Lombok and it is documented here . Removing the annotation should result in correct behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM