fsteeg.com | notes | tags

∞ /notes/encapsulation-jaxb-and-javabeans | 2009-01-06 | java programmming

Encapsulation, JAXB, and JavaBeans

This was not obvious to me: When designing a class to be serialized using JAXB (and getting exceptions about missing getters, setters or no-args constructors), do we need to follow the JavaBeans anti-pattern (the 'anti' is my conclusion from reading Effective Java, 2nd ed., p. 12) and [http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html kiss encapsulation goodbye]? No, you can use a private no-args constructor and let JAXB access private fields directly by annotating the class with: ''@XmlAccessorType(value = XmlAccessType.FIELD)''. No getters, setters, or public fields required.