December 21, 2012

JAXB's @XmlAnyElement(lax=true) Explained

In a previous post I introduced how the @XmlAnyElement(lax=true) annotation could be used to create a very flexible mapping.  In this post I'll dig a bit deeper into how it relates to both the @XmlRootElement and @XmlElementDecl.

December 20, 2012

JAXB - Representing Null and Empty Collections

In this post I will cover how to differentiate between null and empty collections in the XML representation with JAXB (JSR-222).

November 15, 2012

Creating a Generic List Wrapper in JAXB

To marshal/unmarshal a list with a JAXB (JSR-222) implementation you need to create a wrapper object to hold the list. People find this onerous having to create multiple wrapper objects for this purpose.  Below I'll demonstrate that in reality you only need to create one. This post is based on an answer I gave on Stack Overflow.

November 14, 2012

Using JAXB With XSLT to Produce HTML

JAXB (JSR-222) enables Java to treat a domain model as XML.  In this post I will demonstrate how to leverage this by applying an XSLT stylesheet to a JAXB model to produce HTML.  This approach is often leveraged when creating JAX-RS applications.

November 1, 2012

Applying a Namespace During JAXB Unmarshal

For some an XML schema is a strict set of rules for how the XML document must be structured.  But for others it is a general guideline to indicate what the XML should look like.  This means that sometimes people want to accept input that doesn't conform to the XML schema for some reason.  In this example I will demonstrate how this can be done by leveraging a SAX XMLFilter.

October 19, 2012

Updating EclipseLink in WebLogic

JSON binding was added to EclipseLink in version 2.4.  If you are using a version of that does not contain this version (i.e. WebLogic 10.3.4 (11g) contains EclipseLink 2.1.2), then by default you won't have access to this functionality. The recommended solution to this problem is to create a shared library in WebLogic for the newer release of EclipseLink.

August 16, 2012

Removing JAXBElement From Your Domain Model

JAXBElement is a JAXB (JSR-222) mechanism that stores name and namespace information in situations where this can not be determined from the value or mapping.  For example in the class below the elements billing-address and shipping-address both correspond to the Address class.  In order to be able to round trip the data we need to keep track of which element we unmarshalled.
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

public class Customer {

        @XmlElementRef(name = "billing-address"),
        @XmlElementRef(name = "shipping-address")
    private JAXBElement<Address> address;

While useful JAXBElement can get in the way if you want to use your domain model with something like JPA (which doesn't know what to do with it).  In this post I will demonstrate how you can eliminate the need for JAXBElement through the use of an XmlAdapter.

August 12, 2012

Handle the Middle of a XML Document with JAXB and StAX

Recently I have come across a lot of people asking how to read data from, or write data to the middle of an XML document.  In this post I will demonstrate how this can be done using JAXB with StAX.  Note:  JAXB (JSR-222) and StAX (JSR-173) implementations are included in the JDK/JRE since Java SE 6.

August 11, 2012

JAXB's @XmlTransient and Property Order

In previous articles I wrote about how the @XmlTransient annotation can be used at the type level to have a class excluded from the inheritance hierarchy, or at the field/property level to unmap a field/property.  In this article I'll demonstrate how doing this impacts the propOrder setting on the @XmlType annotation.

July 31, 2012

JAXB and Root Elements

@XmlRootElement is an annotation that people are used to using with JAXB (JSR-222).  It's purpose is to uniquely associate a root element with a class.  Since JAXB classes map to complex types, it is possible for a class to correspond to multiple root elements. In this case @XmlRootElement can not be used and people start getting a bit confused.  In this post I'll demonstrate how @XmlElementDecl can be used to map this use case.

July 30, 2012

JAXB - No Annotations Required

There appears to be a misconception that annotations are required on the model in order to use a JAXB (JSR-222) implementation.  The truth is that JAXB is configuration by exception, so annotations are only required when you want to override default behaviour.  In this example I'll demonstrate how to use JAXB without providing any metadata.

June 27, 2012

EclipseLink 2.4 Release Available for Download

On behalf of the MOXy JAXB committers (great job by all), I am very proud to announce that EclipseLink 2.4 has been released and is available for download.  In this post I will summarize what is new in the MOXy component:

May 25, 2012

MOXy as Your JAX-RS JSON Provider - MOXyJsonProvider

In a previous post I demonstrated how you can implement a MessageBodyReader/MessageBodyWriter to leverage  EclipseLink JAXB (MOXy)'s JSON binding in a JAX-RS service.   MOXy now includes an implementation (MOXyJsonProvider) that can be used directly or extended to make the integration even easier.

April 18, 2012

Extending JAXB - Representing Metadata as JSON

In previous posts I have described the XML mapping document and JSON-binding extensions in EclipseLink JAXB (MOXy).   Since MOXy has a JAXB model for its mapping document we were able to eat our own dog food, and now MOXy offers a JSON mapping document.

An external metadata representation is useful when:
  • You cannot modify the domain model (it may come from a 3rd party).
  • You do not want to introduce compile dependencies on JAXB APIs (if you are using a version of Java prior to Java SE 6).
  • You want to apply multiple JAXB mappings to a domain model (you are limited to one representation with annotations).
  • Your object model already contains so many annotations from other technologies that adding more would make the class unreadable.

April 17, 2012

Creating a RESTful Web Service - Part 2/5 (XML Metadata)

Java Persistence Architecture (JPA) is the Java EE standard for mapping POJOs to a relational database. In this example we will use JPA to interact with our database data we set up in part 1.  In the previous post we specified the mapping metadata as annotations.  This post will demonstrate how to specify the same metadata as XML.  One advantage of specifying the metadata as XML is that the domain objects will not have a dependency on the JPA APIs.  This is useful if we want to use the same domain classes on the client side.

April 16, 2012

Binding to JSON & XML - Handling Null

In a previous post I demonstrated how EclipseLink MOXy can be leveraged to produce both XML and JSON representations of your domain model.  The same metadata is used for both representations and MOXy applies it to leverage the capabilities of the media type.  In this post I'll focus on how null is handled in each of these representations.

April 5, 2012

JAXB and Unmapped Properties

JAXB (JSR-222) is configuration by exception, meaning that there is a default mapping applied to domain objects.  This means that sometimes you need to explicitly exclude a field/property.  In this post I'll discuss how this can be done using @XmlTransient or @XmlAccessorType(XmlAccessType.NONE) and when each option is appropriate.

March 15, 2012

MOXy as Your JAX-RS JSON Provider - Client Side

Recently I posted how to leverage EclipseLink JAXB (MOXy)'s JSON binding to create a RESTful service.  In this post I will demonstrate how easy it is to take advantage of MOXy's JSON binding on the client side.

March 13, 2012

MOXy as Your JAX-RS JSON Provider - Server Side

In a previous series of posts I covered how EclipseLink JAXB (MOXy) can be leveraged to create a RESTful data access service.  In this post I will cover how easy it is to leverage MOXy's JSON binding on the server side to add support for JSON messages based on JAXB mappings.
MOXy now includes an implementation of MessageBodyReader/MessageBodyWriter to make it even easier to leverage MOXy's JSON binding in a JAX-RS application.

February 29, 2012

GlassFish 3.1.2 is Full of MOXy (EclipseLink JAXB)

I am very happy to announce that EclipseLink JAXB (MOXy) is now a JAXB (JSR-222) provider in GlassFish 3.1.2.  I would like to thank the EclipseLink and GlassFish committers for all their hard work to make this happen.

In this post I will introduce how MOXy can be leveraged to create a JAX-WS service.  In future posts I will cover more of the extensions in greater detail.

GlassFish can be downloaded from the following link:

February 16, 2012

JAXB and Package Level XmlAdapters

The XmlAdapter mechanism in JAXB (JSR-222) allows an unmappable class to be converted to a mappable one.  An XmlAdapter can be registered with the @XmlJavaTypeAdapter at the field, property, type, or package level.  This post will focus on the scope of the XmlAdapter when registered at the package level.

February 11, 2012

Mapping an Arbitrary List of Objects using JAXB's @XmlAnyElement and XmlAdapter

The @XmlAnyElement annotation enables a property to handle arbitrary XML elements, and the XmlAdapter provides a way to convert an object that can not be mapped into one that can.  In this post we will combine these two mechanisms  to map a list of arbitrary objects.

This post will cover the following concepts:
  1. The @XmlAnyElement annotation
  2. A type level XmlAdapter
  3. Marshalling/Unmarshalling root level simple data types (i.e. String and Integer)
  4. Specifying a root element via JAXBElement
  5. Specifying the type to be unmarshalled on the Unmarshaller

February 9, 2012

JAXB's @XmlType and propOrder

In this post I will demonstrate how to use the propOrder property on the @XmlType annotation to control the ordering of XML elements.  I will also discuss the impact of @XmlAccessorType on how propOrder is configured.

February 6, 2012

JAXB and Inheritance - EclipseLink MOXy's @XmlClassExtractor

In a previous post I covered how JAXB's XmlAdapter could be leveraged to use a node unique to the subtype as the inheritance indicator.  In this post I'll demonstrate that if that node is an XML attribute, then EclipseLink JAXB (MOXy)'s @XmlClassExtractor could be leveraged to do the same thing in fewer lines of code.

January 25, 2012

JAXB and Inheritance - Using XmlAdapter

In previous posts I have covered how to map inheritance relationships in JAXB. This can be done by element name (via @XmlElementRef), by the xsi:type attribute, or in EclipseLink MOXy using another XML attribute (via @XmlDescriminatorNode/@XmlDescriminatorValue).  In this post the type indicator will be an XML attribute/element unique to that type, and we will leverage an XmlAdapter to implement this behaviour.

January 24, 2012

How Does JAXB Compare to XMLBeans?

In previous posts I compared JAXB (JSR-222) to Simple and XStream when starting from Java objects.  In this post I'll compare JAXB to XMLBeans when starting from an XML schema.   I will use XMLBeans 2.5.0 (December 2009) which is the latest release.