JAXB Annotation Example

This tutorial shows you how to use several important annotations via creating JAXB Annotation Example. These annotations ared used frequently in JAXB2 projects recently. You are easy to convert Entities to XML or JSON format. And you will catch up quickly knowledge about JAXB2 frameworks.

Table of contents:
1. Creaete Mapped Java Class
2. JAXB2 annotations
3. Create Main class
4. Demo JAXB Annotation Example

JAXB Annotation Example

Now we are together looking at the mapped java class and the XML it will generate. Then we will focus on each annotation that is used in the java class.

Create Mapped Java Class

JAXB2 annotations

The @XmlRootElement is used to map a class or an enum type to an XML element. The @XmlRootElement annotation can be used with the elements like a top level class or an enum type. When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document. By default class name is used as the root element, but you can specify a different name to be used via name attribute.

XML document for example:

@XmlType
@XmlType specifies the order in XML element that will order in generated XML. You can specify the order using propOrder property. The XML element is generated with the order same as in propOrder property.

@XmlAccessorType
JAXB help us configure the use of fields or properties to access the data in your domain objects. This is specified as an XmlAccessType (PUBLIC_MEMBER, PROPERTY, FIELD, or NONE) via the @XmlAccessorType annotation. The @XmlAccessorType annotation can be used at the type level to configure a single class, or at the package level to change the default access type for all classes within that package. Package level annotations can be supplied through a package-info class.

@XmlAttribute
The @XmlAttribute maps a java bean property or field to an XML attribute. In our above example, we have used id property to map with the id attribute in xml document.

@XmlElement
The @XmlElement maps a java bean property or field to an XML Element, By default, once a class is annotated with @XmlRootElement at class level, all properties or fields will be treated as XML element with name as java property name

The @XmlElement maps a java bean property or field to an XML Element. By default , once @XmlRootElement is declared on class level, all properties(or fields) will be treated as XML element with name as java-bean property name unless they are decared otherwise, or are transient.
For example:
name java property is mapped to name element in XML.

And you can use a different name by using name attribute if you prefer:

importedDate java property is mapped to imported-date element in XML.

@XmlElementWrapper
The @XmlElementWrapper is used to create a wrapper around collection of elements. In our case, we have created a wrapper all-countries around madeCountry collection.

maps to

@XmlList
@XmlList can be used to represent a collection of elements as space separated text.

maps to

@XmlTransient
@XmlTransient prevents a java bean property to be mapped in XML. If a property is annotated with @XmlTransient, this property will not be appear in generated XML.

In our case, property flavor is not present in generated XML.

@XmlJavaTypeAdapter
@XmlJavaTypeAdapter is used to map with the adapter that we can use to convert Java type to XML type and vice versa. In this example, we can implement this adapter like this

maps to

Create Main class
We need to create the main class to demonstrate all above annotations.

Demo JAXB Annotation Example
Running the above main class on Eclipse, we will get the output like this:

JAXB Annotation Example

That’s it. Now you can create your own JAXB Annotation Example.

Download the complete source code, click link below

JaxbAnnotationsExample.zip

Leave a Comment

*

Please share it if you found this useful
Hide Buttons