|
|
Line 1: |
Line 1: |
− | = XmlElementWrapper and empty and null Collections =
| + | Test |
− | | + | |
− | In EclipseLink 2.4, MOXy's handling of empty and null Collections annotated with '''XmlElementWrapper''' has changed, to mirror the functionality of the JAXB reference implementation.
| + | |
− | | + | |
− | * Empty Collections annotated with '''XmlElementWrapper''' will now be marshalled as an empty wrapper element.
| + | |
− | | + | |
− | * Null Collections annotated with '''XmlElementWrapper(nillable = true)''' will now be marshalled as an wrapper element with '''xsi:nil="true"'''.
| + | |
− | | + | |
− | The examples below will use the following domain class:
| + | |
− | | + | |
− | <div style="width:800px">
| + | |
− | <source lang="java">
| + | |
− | @XmlRootElement
| + | |
− | @XmlAccessorType(AccessType.FIELD)
| + | |
− | public class PurchaseOrder {
| + | |
− | | + | |
− | String id;
| + | |
− |
| + | |
− | String customerName;
| + | |
− |
| + | |
− | @XmlElementWrapper(name = "line-items", nillable = true)
| + | |
− | List<LineItem> item = null;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | </div>
| + | |
− | | + | |
− | | + | |
− | == Null Collections ==
| + | |
− | | + | |
− | <div style="width:800px">
| + | |
− | <source lang="java">
| + | |
− | PurchaseOrder po = new PurchaseOrder();
| + | |
− | po.customerName = "Sarah Connor";
| + | |
− | po.id = "FDKJHG387SKUH29887";
| + | |
− | | + | |
− | marshaller.marshal(po, System.out);
| + | |
− | </source>
| + | |
− | </div>
| + | |
− | | + | |
− | The null collection will be marshalled as an empty wrapper element with '''xsi:nil="true"''':
| + | |
− | | + | |
− | <div style="width:800px">
| + | |
− | <source lang="xml">
| + | |
− | <?xml version="1.0" encoding="UTF-8"?>
| + | |
− | <purchaseOrder>
| + | |
− | <id>FDKJHG387SKUH29887</id>
| + | |
− | <customerName>Rick Barkhosue</customerName>
| + | |
− | <line-items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
| + | |
− | </purchaseOrder>
| + | |
− | </source>
| + | |
− | </div>
| + | |
− | | + | |
− | In versions of EclipseLink prior to 2.4, the '''line-items''' element would be absent.
| + | |
− | | + | |
− | | + | |
− | == Empty Collections ==
| + | |
− | | + | |
− | <div style="width:800px">
| + | |
− | <source lang="java">
| + | |
− | PurchaseOrder po = new PurchaseOrder();
| + | |
− | po.customerName = "Sarah Connor";
| + | |
− | po.id = "FDKJHG387SKUH29887";
| + | |
− | po.item = new ArrayList<LineItem>();
| + | |
− | | + | |
− | marshaller.marshal(po, System.out);
| + | |
− | </source>
| + | |
− | </div>
| + | |
− | | + | |
− | The empty collection will be marshalled as an empty wrapper element:
| + | |
− | | + | |
− | <div style="width:800px">
| + | |
− | <source lang="xml">
| + | |
− | <?xml version="1.0" encoding="UTF-8"?>
| + | |
− | <purchaseOrder>
| + | |
− | <id>FDKJHG387SKUH29887</id>
| + | |
− | <customerName>Rick Barkhosue</customerName>
| + | |
− | <line-items/>
| + | |
− | </purchaseOrder>
| + | |
− | </source>
| + | |
− | </div>
| + | |
− | | + | |
− | In versions of EclipseLink prior to 2.4, the '''line-items''' element would be absent.
| + | |
− | | + | |
− | | + | |
− | == Configuration ==
| + | |
− | | + | |
− | If you would like to keep the old EclipseLink behaviour, you can use an '''XmlNullPolicy''' annotation:
| + | |
− | | + | |
− | Using Annotations:
| + | |
− | <div style="width:800px">
| + | |
− | <source lang="java">
| + | |
− | @XmlElementWrapper(name="line-items", nillable=true)
| + | |
− | @XmlNullPolicy(shouldMarshalEmptyCollections=false)
| + | |
− | List<LineItem> item = null;
| + | |
− | </source>
| + | |
− | </div>
| + | |
− | | + | |
− | Using EclipseLink XML Bindings:
| + | |
− | <div style="width:800px">
| + | |
− | <source lang="xml">
| + | |
− | <xml-element java-attribute="item"/>
| + | |
− | <xml-element-wrapper name="line-items" nillable="true" />
| + | |
− | <xml-null-policy should-marshal-empty-collections="false" />
| + | |
− | </xml-element>
| + | |
− | </source>
| + | |
− | </div>
| + | |
− | | + | |
− | By specifying '''(shouldMarshalEmptyCollections = false)''', neither null nor empty Collections will appear in marshalled XML.
| + | |