JAXB provides an easy way to convert XML or JSON representations of data into Java objects and vice versa. Using simple annotations, you can have a JAXB implementation create XML or JSON data from a Java object or create a Java object from XML or JSON.
To understand how Java data types are mapped to XML schema types in JAXB, refer to https://docs.oracle.com/javase/tutorial/jaxb/intro/bind.html.
The following are a few important JAXB annotations:
- @XmlRootElement: This annotation specifies the root element of the XML document and is typically used at the class level.
- @XmlElement: This annotation specifies an XML element that is not a root element. Java class members can be marked as XMLElement when the class is annotated with @XmlRootElement.
- @XmlAttribute: This annotation marks a member of the Java class as an attribute of the parent XML element.
- @XmlAccessorType: This annotation is specified at the class level. It lets you control how class fields are serialized to XML or JSON. Valid values are XmlAccessType.FIELD (every non-static and non-@XmlTransient field is serialized), XmlAccessType.PROPERTY (every pair of getter/setter that is not annotated with @XmlTransient is serialized), XmlAccessType.NONE (no fields are serialized, unless specific fields are annotated for serialization), and XmlAccessType.PUBLIC_MEMBER (all public getter/setter pairs are serialized, unless annotated with @XmlTransient).
- @XMLTransient: This annotation specifies a member or getter/setter pair that is not to be serialized.
For the complete list of JAXB annotations, refer to https://jaxb.java.net/tutorial/section_6_1-JAXB-Annotations.html#JAXB.