Jump to: navigation, search

EclipseLink/Examples/MOXy/Spring/JAXBAnnotations

In order to use EclipseLink JAXB with the Spring Framework, you simply need a jaxb.properties file and an eclipselink.jar on the classpath. No other special configuration is required. This example will demonstrate how to configure Spring to use EclipseLink JAXB.

The latest version of EclipseLink can be found on the EclipseLink download page.

Configuration: applicationContext.xml

The following file will be used to configure the beans used in this example:

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="example.gettingstarted"/>
    </bean>
    <bean id="xmlHelper" class="example.gettingstarted.XMLHelper">
        <property name="marshaller" ref="jaxbMarshaller"/>
    </bean>
</beans>

Two beans are being defined here:

  • xmlHelper
    • This is the class that will do all of the work, i.e. marshal and unmarshal
  • jaxbMarshaller
    • This is an instance of the org.springframework.oxm.jaxb.Jaxb2Marshaller class that will be injected into our xmlHelper
    • We use the "contextPath" property to indicate the location of the model classes, jaxb.properties, and an ObjectFactory class or jaxb.index file

Following is the jaxb.properties file that tells Spring to use EclipseLink JAXB:

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

Bootstrapping the Application

The standard Spring bean lookup method can be used to gain access to the xmlHelper bean:

// initialize IoC Container
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// retrieve the XMLHelper instance from the Container
XMLHelper xmlHelper = (XMLHelper) appContext.getBean("xmlHelper");

Java Source Files

Following is the XMLHelper and model classes used in the example.

example.gettingstarted.XMLHelper.java

package example.gettingstarted;
 
import java.io.IOException;
 
import javax.xml.transform.Result;
import javax.xml.transform.Source;
 
import org.springframework.oxm.XmlMappingException;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
 
public class XMLHelper {
    private Jaxb2Marshaller marshaller;
 
    /**
     * Unmarshal a given source
     */
    public Object load(Source source) throws XmlMappingException, IOException {
        return marshaller.unmarshal(source);
    }
 
    /**
     * Marshal a given Object to a Result
     */
    public void save(Object obj, Result result) throws XmlMappingException, IOException {
        marshaller.marshal(obj, result);
    }
 
    /**
     * This method is used by Spring to inject an instance of Jaxb2Marshaller
     */
    public void setMarshaller(Jaxb2Marshaller marshaller) {
        this.marshaller = marshaller;
    }
}

example.gettingstarted.Customer.java

package example.gettingstarted;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
import org.eclipse.persistence.oxm.annotations.XmlPath;
 
@XmlRootElement
@XmlType(propOrder={"name", "address", "phoneNumbers"})
public class Customer {
    private String name;
    private Address address;
    private List<PhoneNumber> phoneNumbers;
 
    public Customer() {
        phoneNumbers = new ArrayList<PhoneNumber>();
    }
 
    @XmlPath("personal-info/name/text()")
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    @XmlPath("contact-info/address")
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    @XmlPath("contact-info/phone-number")
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }
 
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
}

example.gettingstarted.Address.java

package example.gettingstarted;
 
public class Address {
    private String street;
    private String city;
 
    public String getStreet() {
        return street;
    }
 
    public void setStreet(String street) {
        this.street = street;
    }
 
    public String getCity() {
        return city;
    }
 
    public void setCity(String city) {
        this.city = city;
    }
}

example.gettingstarted.PhoneNumber.java

package example.gettingstarted;
 
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
 
public class PhoneNumber {
    private String type;
    private String value;
 
    @XmlAttribute
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    @XmlValue
    public String getValue() {
        return value;
    }
 
    public void setValue(String value) {
        this.value = value;
    }
}