The most generic way to convert from JSON to Java objects is to capture the JSON as a Map<String, Object>
. From this (or directly) I can convert to a POJO easily:
User user = objectMapper.convertValue(jsonMap, User.class);
Any User
fields that were not present in the original JSON will be set to their default values. While this is great for creating new objects (in response to (say) an API call), it doesn't work for updating a POJO.
What I want to achieve is to update an existing User
object from incoming JSON using Jackson. This means:
- Properties in the incoming JSON (that are also fields in the POJO) are used to update (overwrite) the corresponding fields in the POJO.
- Properties in the incoming JSON that are not fields in the POJO are ignored.
- Fields in the
User
POJO that don't have any corresponding properties in the incoming JSON are left alone.
Is this possible with Jackson?