July 20, 2010

JAXB - The XML Binding Standard

Many people think of JAXB as an implementation (the reference implementation included in Java SE 6).  In reality JAXB is a Java Community Process (JCP) specification with multiple implementations (such as EclipseLink MOXy).  In this post I'll discuss the importance of JAXB the standard.


XML binding solutions existed before JAXB (TopLink OX for example).  Each implementation had its own strengths, but each also contained mundane concepts like mapping a property to an XML attribute.  In one sense JAXB represents the commoditized behavior, where all the participants agreed on a common way to specify common behaviour.  This makes it easier on the user, since when switching between implementations they only need to learn the extensions.

import javax.xml.bind.annotation.XmlAttribute;

public class Customer {

   @XmlAttribute
   private long id;

}

JAXB 2 achieved the goal of representing 100% of XML schema constructs.  Here JAXB serves as a contract that it can handle everything you need to do.  Most XML binding solutions do not cover all XML schema constructs with the goal of remaining "simple', which is fine until you need to do something real.

Being standard makes it easier for other standards to integrate with you.  This is why JAXB is included in Java SE 6 and is the standard binding layer for JAX-WS (Web Services), JAX-RS (RESTful Web Services), and SCA (Service Component Architecture).  If you already know JAXB you are well on your way to being able to use any of the standard service oriented architectures.  This also means there are alot of people using JAXB.

It is easy to switch between JAXB implementations.  Reasons for switching to another JAXB implementation include: performance improvements, and extend features.  No need to re-write your application, simply add a file named jaxb.properties in with your model classes that specifies the JAXB implementation to be used.  Below is an example specifying the MOXy JAXB implementation:


javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

The majority of this blog will be dedicated to JAXB and useful extensions from the MOXy implementation.

No comments:

Post a Comment