Hy,
What I have:
A map with current values
{ "name": "Cedrik", "surname": "MySurname", }
A public class myPojo
@JsonProperty(required = true) private String name;
@JsonProperty(required = true) private String surname;
@JsonProperty(required = true) private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
What I want is to make the conversion from Map to POJO to fail(throw an exception) if one field from the map is not fulfilling the POJO.
It doesn't seem to work with any kind of configuration using DeserializationFeature. I tried to tweak all configs that have something to do with null fields
example:
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, false)
objectMapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true);
This should throw an exception but it doesn't:
MyPojo myPojo = MAPPER.convertValue(action.getParams(), MyPojo.class);
Any idea on how I make it to throw an exception during conversion if the age field is missing from the map and required by POJO?
Jackson does not support validation (it's explicitly out of scope), so you need to validate separately after deserialization, for example by using Bean Validation implementation (like Hibernate Validator).