Thursday, July 26, 2012

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@XmlRootElement(name="MemberDetailsRequest", namespace="http://bk.org/memberservice/")
@XmlAccessorType(XmlAccessType.FIELD)
public class MemberDetailsRequest {
 
 public MemberDetailsRequest() {
 }
 
 public MemberDetailsRequest(Long id) {
  this.id = id;
 }
 
 private Long id;
 
 public Long getId() {
  return id;
 }
 
 public void setId(Long id) {
  this.id = id;
 }
}

and the test for marshalling:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.bk.memberservice.binding;
 
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
 
import java.io.StringWriter;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
 
import org.bk.memberservice.message.MemberDetailsRequest;
import org.junit.Test;
 
public class TestJaxbRequestBinding {
 
 @Test
 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();
  memberDetailsRequest.setId(1L);
  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:

<repository>
            <id>EclipseLink Repo</id>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
            <name>EclipseLink Repo</name>
        </repository>

  <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>2.4.0</version>
        </dependency>

The next step is to instruct to set MOXy as the JAXB provider. This is done by placing a jaxb.properties along with any of the JAXB model classes. So in this case, I have placed a jaxb.properties 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:
Blaise Dougan's Blog: http://blog.bdoughan.com/ and his very enthusiastic participation in Stack Overflow

No comments:

Post a Comment