我需要在jaxb中将null值显示为空元素。我正在使用jaxb的moxy实现。我找到了这个选项。
@XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE)
是否有类级别的类似扩展可以应用于其中定义的所有元素?
我强烈建议用缺少节点或使用xsi:nil =“true”
属性来表示 null
。这在模式验证中效果最佳(即,类型为xsd:int
的元素不是<age/>
或<age></age>
),但如果您不能这样做,则可以通过以下方法完成您的用例:
标准JAXB行为
使用标准API,您可以使用@XmlElement
注释控制是否将null表示为缺少节点或带有xsi: nil =“ true”
(请参见:http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html)。
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
private String street;
@XmlElement(nillable=true)
private String city;
}
如果这两个字段的值都为空,则以下是XML输出。
<?xml version="1.0" encoding="UTF-8"?>
<address>
<city xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</address>
MOXy - 按类别覆盖该行为
MOXy没有提供注释来指定类别上所有属性的null策略。然而,你可以通过使用@XmlCustomizer
注释来利用DescriptorCustomizer
并调整原生MOXy映射元数据来实现相同的功能。
DescriptorCustomizer (AddressCustomizer)
import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
public class AddressCustomizer implements DescriptorCustomizer {
@Override
public void customize(ClassDescriptor descriptor) throws Exception {
for(DatabaseMapping mapping : descriptor.getMappings()) {
if(mapping.isAbstractDirectMapping()) {
XMLDirectMapping xmlDirectMapping = (XMLDirectMapping) mapping;
xmlDirectMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE);
xmlDirectMapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
}
}
}
}
领域模型(地址)
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlCustomizer(AddressCustomizer.class)
public class Address {
private String street;
@XmlElement(nillable=true)
private String city;
}
输出
<?xml version="1.0" encoding="UTF-8"?>
<address>
<street/>
<city/>
</address>
MOXy - 覆盖所有类的行为
如果您想要覆盖所有映射类的空值处理行为,我建议使用 SessionEventListener
。如果您愿意,您也可以使用此方法更新单个类的元数据。
SessionEventListener (NullPolicySessionEventListener)
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
import org.eclipse.persistence.sessions.*;
public class NullPolicySessionEventListener extends SessionEventAdapter {
@Override
public void preLogin(SessionEvent event) {
Project project = event.getSession().getProject();
for(ClassDescriptor descriptor : project.getOrderedDescriptors()) {
for(DatabaseMapping mapping : descriptor.getMappings()) {
if(mapping.isAbstractDirectMapping()) {
XMLDirectMapping xmlDirectMapping = (XMLDirectMapping) mapping;
xmlDirectMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE);
xmlDirectMapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
}
}
}
}
}
演示代码
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.sessions.SessionEventListener;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
SessionEventListener sessionEventListener = new NullPolicySessionEventListener();
properties.put(JAXBContextProperties.SESSION_EVENT_LISTENER, sessionEventListener);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Address.class}, properties);
Address address = new Address();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(address, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8"?>
<address>
<street/>
<city/>
</address>
public class Address {
private String street;
@XmlElement(name = "street")
public void setStreet(String street){
this.street = street;
if (this.street == null){
this.street = ""; // Not NULL, empty string like new String()!
}
}
}
这个方法不能处理日期或数字等其他类型!但有时候字符串就足够了。
如果你能处理好的话,@Blaise Doughan的答案在长期来看似乎更好。:)
Address address = null
),我希望我的输出是<address/>
,但它根本没有出现。有什么帮助吗? - Andrei Manolache