EclipseLink MOXy as a JAXB provider

EclipseLink MOXy is a JAXB provider and is a compelling alternative to the default JAXB provider built into JDK.

First a simple test to marshal a Java object to XML:

This is the model:

@XmlRootElement(name='MemberDetailsRequest', namespace='')
public class MemberDetailsRequest {

 public MemberDetailsRequest() {

 public MemberDetailsRequest(Long id) { = id;

 private Long id;

 public Long getId() {
  return id;

 public void setId(Long id) { = id;

and the test for marshaling:

package org.bk.memberservice.binding;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.bk.memberservice.message.MemberDetailsRequest;
import org.junit.Test;

public class TestJaxbRequestBinding {

 public void testJaxbBinding() throws Exception{
  JAXBContext jaxbContext = JAXBContext.newInstance(MemberDetailsRequest.class);
  assertThat((jaxbContext instanceof org.eclipse.persistence.jaxb.JAXBContext), is(true));
  Marshaller marshaller = jaxbContext.createMarshaller();
  MemberDetailsRequest memberDetailsRequest = new MemberDetailsRequest();
  StringWriter writer = new StringWriter();
  marshaller.marshal(memberDetailsRequest, writer);
  String marshalledXml = writer.toString();
  assertThat(marshalledXml, containsString('MemberDetailsRequest'));

The highlighted line checks to make sure that MOXy’s JAXBContext is the one created.

So to use MOXy as the JAXB provider, first step is to get the jar files,

I had a little difficulty finding the MOXy jars to be used in a Maven pom file, this is what worked for me:

            <id>EclipseLink Repo</id>
            <name>EclipseLink Repo</name>


The next step is to instruct to set MOXy as the JAXB provider. This is done by placing a along with any of the JAXB model classes. So in this case, I have placed a into my package holding MemberDetailsRequest class, and the contents are the following:

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

and that’s it!

Reference: EclipseLink MOXy as a JAXB provider from our JCG partner Biju Kunjummen at the all and sundry blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Leave a Reply

− six = 2

Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: