This is quite straight forward. What is the minimum required structure for a Java POJO to be marshalled as a JSON?
Can you marshall an object as a JSON if it has only getters/setters or are the field declarations mandatory?
Setter/Getter example:
class Circle{
private float radius;
private float pi;
// setter and getters for those aboce;
public float getArea(){
// returns the computed area;
}
}
So can such an object be marshalled as a JSON if the “area” field is not defined in the Foo class as a field? Or is it mandatory to explicitly declare all fields in your POJO?
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
Circle
For MOXy the only annotation required would be
@XmlElementon theareaproperty as there is no setter corresponding to the getter. The@XmlElementis included in Java SE 6 and above:Demo
Output
For More Information