Search code examples
jackson

jackson pass 0 to constructor instead of throw exception when property is absent


I have request as

{
 "name" : "test"
}

RequestDTO like this

data class DTO (
val name: String
val maxValue: Long
)

expected : Json serialisation fail due to missing property maxValue. what is happening: Jackson sets maxValue = 0;

I tried to use property DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES but
IT DOESN'T WORK IF PROPERTY IS ABSENT. IT WORKS ONLY IF YOU HAVE EXPLICIT NULL ON REQUESTS

Jackson deserialization of missing JSON number value to 0.0 in Spring has wrong answer

@JsonProperty(required = true)

Isn't a solution because I need global configuration to prohibit convert absence value to zeros.


Solution

  • You can use

    jacksonObjectMapper.enable(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES)
    

    It makes error slightly different but you will receive 400 bad requests as expected