Java 使用JAXB将空列表封送为缺少的节点
使用JAXB,我希望能够将空列表封送为不存在的节点。我认为日食莫西有这种可能性,但我无法让它发挥作用 根据:你应该可以这样做:Java 使用JAXB将空列表封送为缺少的节点,java,xml,jaxb,moxy,Java,Xml,Jaxb,Moxy,使用JAXB,我希望能够将空列表封送为不存在的节点。我认为日食莫西有这种可能性,但我无法让它发挥作用 根据:你应该可以这样做: @XmlElementWrapper(name="line-items", nillable=true) @XmlNullPolicy(shouldMarshalEmptyCollections=false) List<LineItem> item = null; 不是有效的属性 我尝试过使用eclipselink 2.4.0、2.4.1和2.5.0-M4
@XmlElementWrapper(name="line-items", nillable=true)
@XmlNullPolicy(shouldMarshalEmptyCollections=false)
List<LineItem> item = null;
不是有效的属性
我尝试过使用eclipselink 2.4.0、2.4.1和2.5.0-M4。我做错了什么?您可以使用的@XmlPath
映射来映射这个用例。我将用下面的示例演示它与使用@xmlementwrapper
的比较
根目录
package forum13268598;
import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElementWrapper(name="line-items-element-wrapper")
List<LineItem> item1 = null;
@XmlPath("line-items-xml-path/item1")
List<LineItem> item2 = null;
}
演示
package forum13268598;
import java.util.ArrayList;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
root.item1 = new ArrayList<LineItem>();
root.item2 = new ArrayList<LineItem>();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
package forum13268598;
import java.util.ArrayList;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
root.item1 = new ArrayList<LineItem>();
root.item2 = new ArrayList<LineItem>();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<line-items-element-wrapper/>
</root>