I have a set of classes structured as follows:
public class A implements Serializable {
String prop1;
String prop2;
B prop3;
}
public class B implements Serializable {
String nestedProp4;
String nestedProp5;
C nestedProp6;
}
public class C implements Serializable {
String nestedProp7;
}
I need to serialize an instance of class A. Here is the code I’m using:
A obj = new A();
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.valueToTree(obj);
The actual result of the serialization is:
{
"prop1": null,
"prop2": null,
"prop3": null
}
However, I need the result to include the nested structure with null values for the properties, like this:
{
"prop1": null,
"prop2": null,
"prop3": {
"nestedProp4": null,
"nestedProp5": null,
"nestedProp6": {
"nestedProp7": null
}
}
}
Is there an automatic way or configuration annotation to achieve this serialization without using reflection or setting default values?
Additionally, I have many properties in these classes, so I prefer not to add annotations to each property. Annotations at the top of the classes are allowed.