Difference between revisions of "EclipseLink/Examples/MOXy/GettingStarted/JAXBCustomizations"

From Eclipsepedia

Jump to: navigation, search
m (Overview)
m (Customizing a Type)
Line 4: Line 4:
 
==Customizing a Type==
 
==Customizing a Type==
  
In the previous example (The Basics) we required a supplementary object to serve as the root of our model.  When can use JAXB annotations to specify which classes in our domain model can serve as roots, this is done using the @XmlRootElement annotation.  Now that this annotation has been added we no longer require the use of the JAXBElement class.
+
In the previous example ([[EclipseLink/Examples/MOXy/GettingStarted/TheBasics|The Basics]]) we required a supplementary object to serve as the root of our model.  When can use JAXB annotations to specify which classes in our domain model can serve as roots, this is done using the @XmlRootElement annotation.  Now that this annotation has been added we no longer require the use of the JAXBElement class.
  
 
By default the order in which the XML elements are marshalled out is dependent on the implementation.  We can control the order using the propOrder property on the JAXB annotation @XmlType.
 
By default the order in which the XML elements are marshalled out is dependent on the implementation.  We can control the order using the propOrder property on the JAXB annotation @XmlType.

Revision as of 15:45, 8 January 2010

Contents

Overview

This example will build upon the lessons learned in the previous example (The Basics), and demonstrate how JAXB annotations can be used to customize the XML output.

Customizing a Type

In the previous example (The Basics) we required a supplementary object to serve as the root of our model. When can use JAXB annotations to specify which classes in our domain model can serve as roots, this is done using the @XmlRootElement annotation. Now that this annotation has been added we no longer require the use of the JAXBElement class.

By default the order in which the XML elements are marshalled out is dependent on the implementation. We can control the order using the propOrder property on the JAXB annotation @XmlType.

package example.gettingstarted;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
@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>();
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }
 
    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }
 
}

Customizing a Property

By default Java properties are mapped to XML elements. In order to map a property to an XML attribute the JAXB annotation @XmlAttribute is used. If you want to map a property directly to a text field without an element wrapper simply use the JAXB annotation @XmlValue.

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

Converting Objects to XML

The following code is used to convert the objects to XML. Note that unlike in the previous example (The Basics) we do not need to wrap the Customer object in a JAXBElement.

package example.gettingstarted;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
 
public class Demo {
 
    public static void main(String[] args) throws JAXBException {
 
        // Step 1 - Create the Domain Model
 
        Customer customer = new Customer();
        customer.setName("Jane Doe");
 
        Address address = new Address();
        address.setStreet("123 Any Street");
        address.setCity("My Town");
        customer.setAddress(address);
 
        PhoneNumber workPhoneNumber = new PhoneNumber();
        workPhoneNumber.setType("work");
        workPhoneNumber.setValue("613-555-1111");
        customer.getPhoneNumbers().add(workPhoneNumber);
 
        PhoneNumber cellPhoneNumber = new PhoneNumber();
        cellPhoneNumber.setType("cell");
        cellPhoneNumber.setValue("613-555-2222");
        customer.getPhoneNumbers().add(cellPhoneNumber);
 
        // Step 2 - Convert the Domain Model to XML
 
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
 
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
        marshaller.marshal(customer, System.out);
 
    }
 
}

XML Output

The following is the resulting XML.

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