Wednesday, May 19, 2010

XmlAccessType.FIELD vs XmlAccessType.PROPERTY

As a Java guy, I feel disturbed by using XmlAccessType.FIELD
(see )
in JAXB while "exposing" a JavaBean as a XML data type.

XmlAccessType.FIELD will expose ANY Java attribute, even the private ones, even those that you didn't mean to expose, UNLESS you annotate them as @XmlTransient .

MY vision, which I assume matches the JavaBean vision, is that you expose only properties which are explicitly associated with a getter (setter if they are writable), and that you should really never access directly the attribute as a field. This leads to a more method-centric programming style, as opposed to a data-centric programming style.

For this reason, I privilege the XmlAccessType.PROPERTY approach.

1 comment: