-
Notifications
You must be signed in to change notification settings - Fork 99
Annox JAXB User Guide
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.
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>
- Home
- Migration guide
-
JAXB Maven Plugin
- Quick Start
-
User Guide
- Basic Usage
- Specifying What To Compile
- Referencing Resources in Maven Artifacts
- Using Catalogs
- Using Episodes
- Modular Schema Compilation
- Controlling the Output
- Using JAXB Plugins
- Using a Specific JAXB Version
- Configuring Extension, Validation and XML Security
- IDE Integration
- Miscellaneous
- Configuring Proxies
- Maven Documentation
- Configuration Cheat Sheet
- Common Pitfalls and Problems
-
JAXB2 Basics Plugins
- Using JAXB2 Basics Plugins
- JSR-305 Support
-
JAXB2 Basics Plugins List
- SimpleEquals Plugin
- SimpleHashCode Plugin
- Equals Plugin
- HashCode Plugin
- ToString Plugin
- Copyable Plugin
- Mergeable Plugin
- Inheritance Plugin
- AutoInheritance Plugin
- Wildcard Plugin
- Setters Plugin
- Simplify Plugin
- EnumValue Plugin
- JAXBIndex Plugin
- FixJAXB1058 Plugin
- Commons Lang Plugin
- Default Value Plugin
- Fluent API Plugin
- Namespace Prefix Plugin
- Value Constructor Plugin
- Boolean Getter Plugin
- CamelCase Plugin
- XML ElementWrapper Plugin
- Parent Pointer Plugin
- Property Listener Injector Plugin
- Annox
- JAXB Annotate Plugin
-
HyperJAXB3
- Build System Support
- Customization Guide
- Databases
- Development guide
- Extension guide
- FAQ
- IDE Support
- Java Persistence
- JAXB
- JDK Support
- Project Templates
-
Reference
- Adding vendor-specific annotations
- Features
- Integrating Hyperjaxb3 in builds
- Introduction
- Making schema-derived classes ready for JPA
- Adding required properties
- Applying workarounds for JAXB vs. JPA conflicts
- Enforcing top-level classes
- Generating equals and hashCode methods
- Generating ORM metadata
- Generating persistence unit descriptor
- JPA 2 Support
- Making classes serializable
- Testing generated mappings
- Reference - single page
- Related Projects
- Sample projects
- Solutions
- Target Scenarios
- Test Projects
- Tutorials
- Best Practices
- FAQ
- Sample Projects
- Support
- License
- Distribution