Skip to content

Annox JAXB User Guide

Laurent Schoelens edited this page Aug 28, 2023 · 1 revision

Overview

JAXB (Java Architecture for XML Binding) allows mapping Java classes to XML representations.

Starting from version 2.0, JAXB is heavily based on Java annotations to define Java/XML mappings. In order to be able to marshal your objects into XML and umarshall objects from XML, you first need to have your classes annotated with JAXB annotations. You can do this manually with your existing classes or you can use a schema compiler to compile your XML schema into a set of schema-derived classes which will contain all the required annotations.

The problem is that JAXB does not provide any alternatives to reading Java/XML mapping from annotations. If you're using JAXB, you're forced to have your mappings defined with Java annotations - with all the limitations I've mentioned in the user guide. Like, you can't annotate third-party classes, you can't provide alternative annotations for already annotated classes and so on.

Annox provides a solution for this problem.

JAXB reference implementation can be configured with a special annotation reader which may implement a different strategy for reading annotations. Annox takes advantage of this feature and implements an annotation reader which can load JAXB annotations from XML.

Using Annox annotation reader with JAXB RI

Before you start with Annox annotation reader for JAXB RI, I highly recommend reading the Annox user guide in order to understand core Annox concepts.

Using Annox annotation reader with JAXB RI is really very simple. You just have to create an instance of AnnoxAnnotationReader and set it as JAXBRIContext.ANNOTATION_READER property of the JAXB context :

final AnnotationReader<Type, Class, Field, Method> annotationReader = new AnnoxAnnotationReader();

final Map<String, Object> properties = new HashMap<>();

properties.put(JAXBRIContext.ANNOTATION_READER, annotationReader);

final JAXBContext context = JAXBContext.newInstance(
    "org.jvnet.annox.samples.po",
    Thread.currentThread().getContextClassLoader(),
    properties);

final Object myObject = context.createUnmarshaller().unmarshal( ... );

See Purchase Order Sample for an working example.

Here's what mappings look like

  • classpath:org/jvnet/annox/samples/po/package-info.ann.xml :
<package xmlns="http://annox.dev.java.net" xmlns:annox="http://annox.dev.java.net" xmlns:jaxb="http://annox.dev.java.net/javax.xml.bind.annotation"
  xmlns:adap="http://annox.dev.java.net/javax.xml.bind.annotation.adapters">

  <adap:XmlJavaTypeAdapters>
    <adap:XmlJavaTypeAdapter value="org.jvnet.annox.samples.po.BigDecimalAdapter" type="java.math.BigDecimal" />
  </adap:XmlJavaTypeAdapters>

  <class name="ObjectFactory">
    <jaxb:XmlRegistry />
    <method name="createPurchaseOrder">
      <jaxb:XmlElementDecl namespace="" name="purchaseOrder" />
    </method>
    <method name="createComment">
      <jaxb:XmlElementDecl namespace="" name="comment" />
    </method>
  </class>
  <class name="PurchaseOrderType">
    <jaxb:XmlAccessorType value="FIELD" />
    <jaxb:XmlType name="PurchaseOrderType" propOrder="shipTo billTo comment items" />
    <field name="shipTo">
      <jaxb:XmlElement required="true" />
    </field>
    <field name="billTo">
      <jaxb:XmlElement required="true" />
    </field>
    <field name="items">
      <jaxb:XmlElement required="true" />
    </field>
    <field name="orderDate">
      <jaxb:XmlAttribute />
      <jaxb:XmlSchemaType name="date" />
    </field>
  </class>
  <class name="Items">
    <jaxb:XmlAccessorType value="FIELD" />
    <jaxb:XmlType name="Items" propOrder="item" />
  </class>
</package>
  • classpath:org/jvnet/annox/samples/po/Items$Item.ann.xml :
<class xmlns="http://annox.dev.java.net" xmlns:annox="http://annox.dev.java.net" xmlns:jaxb="http://annox.dev.java.net/javax.xml.bind.annotation">
  
  <jaxb:XmlAccessorType value="FIELD"/>
  <jaxb:XmlType name="" propOrder="productName quantity usPrice comment shipDate"/>
  <field name="productName">
    <jaxb:XmlElement required="true"/>
  </field>
  <field name="usPrice">
    <jaxb:XmlElement name="USPrice" required="true"/>
  </field>
  <field name="shipDate">
    <jaxb:XmlSchemaType name="date"/>
  </field>
  <field name="partNum">
    <jaxb:XmlAttribute required="true"/>
  </field>
</class>
Clone this wiki locally