Search code examples
javajacksondeserializationmapper

Jackson Mapper - how to fail on null or empty values


We're using Jackson JSON mapper in our code to de-serialize some configuration objects. We'd like for Jackson to fail on de-serialization when specific fields are missing or empty

The only feature in Jackson to support this behavior is for primitives :

final DeserializationConfig.Feature failOnPremitives = DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES;

The thing is the fields in question are mainly strings

Any help is highly appreciated


Solution

  • There is an option called: FAIL_ON_NULL_FOR_CREATOR_PARAMETERS.

    So I assume it would be accessible at: DeserializationConfig.Feature.FAIL_ON_NULL_FOR_CREATOR_PARAMETERS;

    Or in yml:

    jackson:
        serialization:
          indent-output: false
        deserialization:
          fail-on-unknown-properties: true
          fail-on-missing-creator-properties: true
          fail-on-null-creator-properties: true
    

    This works on all types, strings, ints, doubles etc..