Hi I have the following object mapper:
public <T> T convertJSONtoPOJO(String inputJSON,
Class<T> valueType) throws Exception {
try {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
return objectMapper.readValue(inputJSON, valueType);
} catch (Exception exception) {
//my exception which doesnt matter now
}
}
And incoming JSON:
{
"propertyA": "1",
"propertyB": null,
}
And POJO:
@FieldDefaults(level = AccessLevel.PRIVATE)
@Data
public class MyClazz {
String propertyA;
String propertyB;
String propertyC;
}
By default @JsonProperty required is set to false on all of them.
Now while deserializing I want to obtain POJO which does NOT include: a) non-required variables if they do NOT occur in JSON b) variables which have null value.
Currently my code does NOT fail, it simply gives me the following POJO:
propertyA = 1
propertyB = null
propertyC = null
But I want to obtain POJO with only:
propertyA = 1
If any of your fields are not required, you have to check them whether they are null or else, while using them. Because your class is represented by all the fields you define for it, and there is no way to ignore those fields with null values.
It's possible while serializing and converting to json, but not in deserializing.