Jackson custom filter with full POJO data bind Jackson custom filter with full POJO data bind json json

Jackson custom filter with full POJO data bind


Seems like Json Schema might fit your needs. It allows for flexible (and complex) validation rules of json strings before they are deserialized. It includes mandatory fields, regex-based value check, industry-standard formats (for instance, you can define a field as "email" format), cross-field dependencies (in latest v4), etc

The above is language-independant standard. As for Java implemenation, I used this one which supports latest json schema version (the standard is still evolving). The initial integration of the validator was a lot of work, (becasue of my very-dynamic json model) but after that it is very convinient to introduce new validation rules (just need to change json schema file)


I would recommend to separate concerns for deserialization and validation by using Jackson and Hibernate Vaildator correspondingly. The idea is first to deserialize json data into POJO, and then validate the POJO according to the requirement. In you case, you can apply Class level constraints for validation. Class level constraints have a lot of flexibility and can validate multiple correlated properties by accessing the object instance. It is simple yet powerful.

Usually validation need more high level concerns. It is better to handle this after desrialization. Doing this can make the code more easily to manage and reuse the POJO and validation rules.


just to consider: if you don't care about validation during deserialization, try the @JsonIgnoreProperties(ignoreUnknown = true) annotation for you POJO class. You can do the validation later where actual business logic works with pojo classes.