zarzyk
zarzyk

Reputation: 704

Jackson with JSON: How can I make it ignore additional properties but error on incomplete json?

I know how I can make Jackson to ignore any additional fields in Json, simply by adding @JsonIgnoreProperties(ignoreUnknown = true):

@JsonIgnoreProperties(ignoreUnknown = true)
class MyDto {
    int someField;
}

But side-effect of this is that Jackson now also accepts incomplete JSON and fills missing fields with nulls. How can I enforce Jackson to require every field to exist in json and still ignore additional fields in it?

Thank you.

Upvotes: 1

Views: 2114

Answers (2)

StaxMan
StaxMan

Reputation: 116472

Jackson explicitly does NOT validate logical POJO contents; instead, you are recommended to use Bean Validation (JSR-303, see http://en.wikipedia.org/wiki/Bean_Validation) API implementation; for example one provided by Hibernate project: http://hibernate.org/validator/

This is the approach many frameworks take; for example, DropWizard supports data-binding using Jackson, and then validation (after data-bind, before business logic run) using Bean Validation.

Upvotes: 1

Drejc
Drejc

Reputation: 14286

In order to check if all properties needed are available you need to add the required anotation to the property.

@JsonProperty(value = "response", required = true)
public SomeResponse response;

Upvotes: 0

Related Questions