Jackson – Ignore Unrecognized field / map a json field to an object field with a different name.

When the json string has additional properties than the object, then Jackson throws com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.

To avoid this we can use

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

If you want to map a json field to an object field with a different name we can use @JsonProperty annotation.

For example to map the user-name in the below json String to userName field in the object, we can add @JsonProperty(“user-name”) annotation to the field as shown below.

"{\"user-name\": \"Tim\", \"user-location\":\"US\", \"user-age\": \"32\"}";

@JsonProperty("user-name")
private String userName;

%d bloggers like this: