I try to deserialize a JSON object that I receive in my API using the following code:
ObjectMapper mapper = new ObjectMapper();
ExampleDto ed = mapper.readValue(req.body(), ExampleDto.class);
My class uses Lombok to generate constructors, getters and setters, and looks like this:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExampleDto {
private String name = "";
private List<String> values = new LinkedList<>();
}
Both properties should be optional, and use the default value specified in the class definition if they are not provided. However, if I now try to deserialize the JSON
{name: "Foo"}
the values
field is null
. From my understanding, and all example code I found, values
should be an empty list.
Edit: Not a duplicate, as I'm using Lombok without Optionals