Annotation in JAXB

Connect with

Jaxb annotationsJAXB annotation in Java is way of notifying to JVM to do on behalf of you. Well, in this post I tried to explain different type of Annotations used in JAXB API the annotation as: @XmlRootElement, @XmlType, @XmLElementWrapper, @XmlAttribute, @XmlAccessorType @XmlJavaTypeAdapter

1. Different JAXB Annotations

There are different JAXB annotation used in Java Architecture for XML binding (JAXB), here are some key annotations used in JAXB. If you don’t know the jaxb, you can view my another post , what is JAXB? .
Following are the Annotation Name and its description which will be helpful to understand.

  • @XmlRootElement(namespace = "namespace"): Define the root element for an XML tree
  • @XmlType(propOrder = { "field2", "field1",.. }): Allows to define the order in which the fields are written in the XML file
  • @XmlElement(name = "mytitle"): Define the XML element which will be used. used only if name of ‘mytitle’ is different than the JavaBeans name.
  • @XmLElementWrapper: use to generates a wrapper element around XML representation
  • @XmlAttribute( name = "id" required = true ): used for adding attribute in xml element.
  • @XmlAccessorType(XmlAccessType.FIELD): this is used on class level annotation, for default property name in java and xml element name
  • @XmlJavaTypeAdapter(MyDateAdapter.class): This annotation is used to convert to/from customer java object. like xml date string to , you can see example of date format in JAXB using @XmlJavaTypeAdapter

2. example of @XmlJavaTypeAdapter JAXB Annotation

code snippet of @XmlJavaTypeAdapter(MyDateAdapter.class)

 private Date dateOfBirth;

3. Reference

Official Site
Your comments are welcome to improve this post. Happy Learning! 🙂

Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *