MOXy OXM External Mapping Example

This example demonstrates utilizing MOXy OXM External Mapping extension when dealing with XML representation when developing a Jersey based RESTful Web application

What is MOXy OXM External Mapping Extension

MOXy is EclipseLink's Object to XML Mapping services. MOXy allows for a POJO object model to be mapped to an XML schema. The Java Architecture for XML Binding (JAXB) provides a Java standard for object XML mapping (OXM). MOXy supports JAXB, as well as providing its' own native API and integration with Web Services.

In addition to using Java annotations, EclipseLink provides an XML mapping configuration file called eclipselink-oxm.xml. This mapping file can be used in place of or to override JAXB annotations in source. In addition to allowing all of the standard JAXB mapping capabilities it also includes advanced mapping types and options.

The MOXy extension shown in this example is described on the Eclipse Wiki site at http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML. The extension allows you to process Java beans which do not use JAXB annotations to produce and/or consume XML. To emphasise you do not need access to source code of such beans, the XML Java model classes for this example are placed in a separate module called beans. The other module, webapp, then only contains a JAX-RS web resource, CustomerResource, which uses MOXy to serialize customer XML representation.

Please check out the source code and the wiki page linked above for the detailed information on the OXM external mapping feature.

Replacing Implicit JAXB Runtime With MOXy

Since MOXy is a JAXB implementation, the example still utilizes the standard Jersey JAXB message body reader/writer providers. To make Jersey use MOXy runtime, you just need to put a jaxb.properties file into the Java package containing your JAXB beans. The file should have the following content:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Concrete file used in this example is placed under src/main/resources/com/sun/jersey/samples/moxy/beans/jaxb.properties

So far the configuration was the same as in the other MOXy example in Jersey. Now to enable MOXy JAXB provider also for the types, which do not use JAXB annotations, you need to name respective Java packages in Jersey ResourceConfig property called com.sun.jersey.moxy.config.property.packages. And finally to make Jersey use properly configured JAXB context resolver, you need to add com.sun.jersey.moxy.MoxyContextResolver to your provider classes. In this example we do so via META-INF/services/jersey-server-components and META-INF/services/jersey-client-components placed in the web application WAR file.

REST Resources

The example consists of a single REST Resource represented by the following Java class:

com.sun.jersey.samples.moxy.CustomerResource
A resource class that maintains a single customer data.

The mapping of the URI path space is presented in the following table:

URI path Resource class HTTP methods
/customer CustomerResource GET
/customer CustomerResource PUT

Running the Example

You will need to manually deploy the example web application archive to your web container. The simplest way to try this out from the command line could be:

      cd webapp && mvn clean package embedded-glassfish:run
      
Then you may want to open http://localhost:8080/moxy/customers in your favourite web browser. If you will, go ahead and check the source code to see, that the Java beans in the beans module do not contain any JAXB annotations, and the web application is still able to generate XML out of it.