Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/Spring/JAXBAnnotations"

< EclipseLink‎ | Examples‎ | MOXy‎ | Spring
(example.gettingstarted..java)
(example.gettingstarted.XMLHelperTest.java)
 
(32 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
[[Category:EclipseLink/Examples/MOXy/Spring/JAXBAnnotations]]
 
[[Category:EclipseLink/Examples/MOXy/Spring/JAXBAnnotations]]
  
In order to use EclipseLink JAXB with the Spring Framework, you simply need a <code>jaxb.properties</code> file and an <code>eclipselink.jar</code> on the classpath.  No other special configuration is required.  This example will demonstrate how to configure Spring to use EclipseLink JAXB.
+
In order to use EclipseLink JAXB with the Spring Framework, you simply need a <code>jaxb.properties</code> file and an <code>eclipselink.jar</code> on the classpath.  No other special configuration is required.  This document will demonstrate how to configure Spring to use EclipseLink JAXB.
 
+
The latest version of EclipseLink can be found on the [http://www.eclipse.org/eclipselink/downloads/ EclipseLink download] page.
+
  
 
== Configuration: applicationContext.xml ==
 
== Configuration: applicationContext.xml ==
The following file will be used to configure the beans used in this example:
+
In Spring, beans are configured using the <code>applicationContext.xml</code> file.  The following XML file will be used to configure our beans:
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
Line 26: Line 24:
 
* xmlHelper
 
* xmlHelper
 
** This is the class that will do all of the work, i.e. marshal and unmarshal
 
** This is the class that will do all of the work, i.e. marshal and unmarshal
 +
** We use the "marshaller" property to indicate that we want Spring to inject an instance of <code>org.springframework.oxm.jaxb.Jaxb2Marshaller</code>
 
* jaxbMarshaller
 
* jaxbMarshaller
** This is an instance of the <code>org.springframework.oxm.jaxb.Jaxb2Marshaller</code> class that will be injected into our xmlHelper
+
** This is an instance of the <code>org.springframework.oxm.jaxb.Jaxb2Marshaller</code> class that will be injected into our xmlHelper bean
** We use the "contexPath" property to indicate the location of the model classes, <code>jaxb.properties</code>, and <code>jaxb.index</code>
+
** We use the "contextPath" property to indicate the location of the model classes, <code>jaxb.properties</code>, and an <code>ObjectFactory</code> class or <code>jaxb.index</code> file
  
 
Following is the <code>jaxb.properties</code> file that tells Spring to use EclipseLink JAXB:
 
Following is the <code>jaxb.properties</code> file that tells Spring to use EclipseLink JAXB:
Line 35: Line 34:
 
</source>
 
</source>
  
== Bootstrapping the Example ==
+
== Bootstrapping the Application ==
 
The standard Spring bean lookup method can be used to gain access to the <code>xmlHelper</code> bean:
 
The standard Spring bean lookup method can be used to gain access to the <code>xmlHelper</code> bean:
 
<source lang="java">
 
<source lang="java">
Line 44: Line 43:
 
</source>
 
</source>
  
== Java Source Files ==
+
== Example ==
Following is the XMLHelper and model classes used in the example.
+
Here is an example of EclipseLink JAXB used with the Spring Framework.
  
=== example.gettingstarted.XMLHelper.java ===
+
=== Requirements ===
 +
* EclipseLink
 +
** The latest version of EclipseLink can be found on the [http://www.eclipse.org/eclipselink/downloads EclipseLink download] page. <code>eclipselink.jar</code> must be on the classpath.
 +
* Spring Framework
 +
** The latest version of the Spring Framework can be found on the [http://www.springsource.org/download Spring download] page.
 +
** The JAR files in the <code>dist</code> folder of your Spring install as well as <code>commons-logging.jar</code> found in <code>/projects/spring-build/lib/ivy</code> must be on the classpath.
 +
* Model Classes
 +
** The following model classes make use of standard JAXB annotations as well as MOXy extensions:
 +
*** [http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/MOXyExtensions#Using_MOXy_Extensions Customer]
 +
*** [http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics#Domain_Model Address]
 +
*** [http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/JAXBCustomizations#Customizing_a_Property PhoneNumber]
 +
 
 +
=== Source/Config Files ===
 +
This section contains the various source and configuration files for the example.
 +
 
 +
==== example.gettingstarted.XMLHelper.java ====
 +
This is the class responsible for marshal/unmarshal operations:
 
<source lang="java">
 
<source lang="java">
 
package example.gettingstarted;
 
package example.gettingstarted;
Line 70: Line 85:
 
      
 
      
 
     /**
 
     /**
     * Marshal a given Object to a Result.
+
     * Marshal a given Object to a Result
 
     */
 
     */
 
     public void save(Object obj, Result result) throws XmlMappingException, IOException {
 
     public void save(Object obj, Result result) throws XmlMappingException, IOException {
Line 85: Line 100:
 
</source>
 
</source>
  
=== example.gettingstarted.Customer.java ===
+
This code demonstrates how the XMLHelper bean can be acquired and used to perform marshal/unmarshal operations on a Customer.
 
<source lang="java">
 
<source lang="java">
package example.gettingstarted;
+
// initialize IoC Container
 
+
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
import java.util.ArrayList;
+
// retrieve the XMLHelper instance from the Container
import java.util.List;
+
xmlHelper = (XMLHelper) appContext.getBean("xmlHelper");
 
+
      
import javax.xml.bind.annotation.XmlRootElement;
+
// load Customer
import javax.xml.bind.annotation.XmlType;
+
Customer customer = (Customer) xmlHelper.load(new StreamSource(new FileInputStream("customer.xml")));
 
+
// update Customer
import org.eclipse.persistence.oxm.annotations.XmlPath;
+
PhoneNumber pn = new PhoneNumber();
 
+
pn.setType("Additional");
@XmlRootElement
+
pn.setValue("613-123-1234");
@XmlType(propOrder={"name", "address", "phoneNumbers"})
+
customer.getPhoneNumbers().add(pn);
public class Customer {
+
// save Customer
    private String name;
+
xmlHelper.save(customer, new StreamResult(new FileOutputStream("customer.xml")));
     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;
+
    }
+
}
+
 
</source>
 
</source>
  
=== example.gettingstarted.Address.java ===
+
==== customer.xml ====
<source lang="java">
+
This is a sample instance document.
package example.gettingstarted;
+
<source lang="xml">
 
+
<?xml version="1.0" encoding="UTF-8"?>
public class Address {
+
<customer>
    private String street;
+
  <personal-info>
    private String city;
+
      <name>Jane Doe</name>
+
  </personal-info>
    public String getStreet() {
+
  <contact-info>
        return street;
+
      <address>
    }
+
        <city>My Town</city>
+
        <street>123 Any Street</street>
    public void setStreet(String street) {
+
      </address>
        this.street = street;
+
      <phone-number type="work">613-555-1111</phone-number>
    }
+
      <phone-number type="cell">613-555-2222</phone-number>
+
  </contact-info>
    public String getCity() {
+
</customer>
        return city;
+
    }
+
+
    public void setCity(String city) {
+
        this.city = city;
+
    }
+
}
+
 
</source>
 
</source>
  
=== example.gettingstarted.PhoneNumber.java ===
+
==== jaxb.index ====
<source lang="java">
+
This is the <code>jaxb.index</code> file used by the context to identify the classes it will be responsible for.
package example.gettingstarted;
+
<source lang="xml">
 
+
Customer
import javax.xml.bind.annotation.XmlAttribute;
+
Address
import javax.xml.bind.annotation.XmlValue;
+
PhoneNumber
 
+
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;
+
    }
+
}
+
 
</source>
 
</source>

Latest revision as of 13:37, 26 November 2010

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 document will demonstrate how to configure Spring to use EclipseLink JAXB.

Configuration: applicationContext.xml

In Spring, beans are configured using the applicationContext.xml file. The following XML file will be used to configure our beans:

<?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
    • We use the "marshaller" property to indicate that we want Spring to inject an instance of org.springframework.oxm.jaxb.Jaxb2Marshaller
  • jaxbMarshaller
    • This is an instance of the org.springframework.oxm.jaxb.Jaxb2Marshaller class that will be injected into our xmlHelper bean
    • 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");

Example

Here is an example of EclipseLink JAXB used with the Spring Framework.

Requirements

  • EclipseLink
    • The latest version of EclipseLink can be found on the EclipseLink download page. eclipselink.jar must be on the classpath.
  • Spring Framework
    • The latest version of the Spring Framework can be found on the Spring download page.
    • The JAR files in the dist folder of your Spring install as well as commons-logging.jar found in /projects/spring-build/lib/ivy must be on the classpath.
  • Model Classes

Source/Config Files

This section contains the various source and configuration files for the example.

example.gettingstarted.XMLHelper.java

This is the class responsible for marshal/unmarshal operations:

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;
    }
}

This code demonstrates how the XMLHelper bean can be acquired and used to perform marshal/unmarshal operations on a Customer.

// initialize IoC Container
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// retrieve the XMLHelper instance from the Container
xmlHelper = (XMLHelper) appContext.getBean("xmlHelper");
 
// load Customer
Customer customer = (Customer) xmlHelper.load(new StreamSource(new FileInputStream("customer.xml")));
// update Customer
PhoneNumber pn = new PhoneNumber();
pn.setType("Additional");
pn.setValue("613-123-1234");
customer.getPhoneNumbers().add(pn);
// save Customer
xmlHelper.save(customer, new StreamResult(new FileOutputStream("customer.xml")));

customer.xml

This is a sample instance document.

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <personal-info>
      <name>Jane Doe</name>
   </personal-info>
   <contact-info>
      <address>
         <city>My Town</city>
         <street>123 Any Street</street>
      </address>
      <phone-number type="work">613-555-1111</phone-number>
      <phone-number type="cell">613-555-2222</phone-number>
   </contact-info>
</customer>

jaxb.index

This is the jaxb.index file used by the context to identify the classes it will be responsible for.

Customer
Address
PhoneNumber