Jackson Mapper - how to fail on null or empty values

Ohad Benita picture Ohad Benita · Sep 24, 2017 · Viewed 9k times · Source

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

Answer

cassiomolin picture cassiomolin · Sep 24, 2017

Have you considered Bean Validation?

While Jackson is focused in JSON parsing, Bean Validation is all about declaring and performing validation on your beans.

You could use @NotNull or @NotBlank from Hibernate Validator, the Bean Validation reference implementation.


Alternatively you could use JSON Schema.