Create a custom deserializer using Jackson Object Mapper
When working with complex Json data structures, it is easier to create a custom deserializer to map the json objects rather than having to create many separate models.
The Json
This Json is non trivial since the data required is nested one level deep and is in an array.
Create the model
The @AllArgsConstructor is a Lombok annotation that will create the constructor at runtime.
The @JsonDeserialize is referencing the custom deserializer that is going to be used to populate this model object with the required data
Create the deserializer
The custom deserializer extends the JsonDeseralizer from Jackson Databind and takes the type of the object we are deserializing to, in this case a User object.
We first get the user attribute of the Json as a JsonNode object.
We then check if it is an array and then iterate over the object to get the nested values in the array.
We then return the user object which will be populated with the name and address.
Using the custom deserializer
User user = objectMapper.readValue(<<JSON STRING>>, User.class);
When calling object mapper to deserialize the Json into a User class object, it will use the custom deserializer that we created as the user model class has the the annotation to point to the deserializer.