Jackson - Required property?

Jackson does not include validation functionality, and this is by design (i.e. that is considered out-of-scope). But what is usually used is Bean Validation API implementation. The nice thing about this is decoupling between data format handling, and validation logic. This is what frameworks like DropWizard use; and it's the direction JAX-RS (like Jersey) are taking things for JAX-RS 2.0.


You can mark a property as required with the @JsonProperty(required = true) annotation, and it will throw a JsonMappingException during deserialization if the property is missing or null.

Edit: I received a downvote for this without comment. I'd love to know why, since it does exactly the right thing.


If you want to make sure a json field is provided, you have to use the @JsonProperty(value = "fieldName", required = true) annotation as a parameter to the constructor. But this is not enough, also the Constructor should have @JsonCreator annotation.

For example, if you have a field named 'endPoint' and you want o make sure it is provided in the JSON file, then the following code will throw an exception if it is not provided.

@JsonCreator
public QuerySettings(@JsonProperty(value = "endPoint", required = true) String endPoint) {
        this.endPoint = endPoint;
}

I found this link helpful to understand the Jackson annotations. It also well explains why required=true is not enough and counter-intuitive to its name.