I have a POJO with a field or property, containing collection of objects, something like this:
public class Box {
public List<Items> items;
}
By default, value of items
is null, and I do not want to initialize it with empty list.
Now, if I try to serialize it with Jackson, I get NullPointerException
. Is there a simple way to make Jackson not break on such value and serialize it as an empty collection: [ ]
?
Note. This class is just a simplified example. In reality, there are a hundred of classes and a number of fields with different names in each of them, which are occasionally set to null
sometimes somewhere in the code, breaking serialization in runtime.
Have you considered making this class a JavaBean? In that case, you would be able to give a default value in the getter:
This approach would prevent a lot of trouble related to Jackson's assumptions.
Update: Based on clarification... You could implement a custom serializer for the list type (and/or any other desired customization). Please note that :
Repeat for all such customization.
Code was inspired by this article: http://www.baeldung.com/jackson-custom-serialization
The easiest way to solve this problem is by initializing the List in a default constructor:
If you do not want to change the contract of your POJO class, think about the possibility to define custom Jackson serializer / deserializer which extend JsonSerializer<T> and JsonDeserializer<T> respectively. E.g.:
and then
You can check whether your field is null and act accordingly, in both directions (serialization / deserialization).