java jaxb注解xmlnull_在xml jaxb中将空值表示为空元素

小编典典

我强烈建议null使用不存在节点或使用xsi:nil="true"属性来表示。这与模式验证(即或不是有效的type元素)一起使用时效果最佳xsd:int。但是,如果不能这样做,那么可以完成用例:

标准JAXB行为

使用标准的API,你可以控制NULL是表示为不存在的节点或xsi:nil="true"与@XmlElement标注(见: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;

}

如果两个字段的值均为null,则下面是XML输出。

MOXy-覆盖每个班级的这种行为

MOXy不提供注释来为类的所有属性指定空策略。但是,您可以DescriptorCustomizer通过@XmlCustomizer注释利用并调整本机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);

}

}

}

}

DomainModel(地址)

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;

}

输出量

MOXy-覆盖所有类别的这种行为

相反,如果您想覆盖所有映射类的空处理,我建议使用a 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 properties = new HashMap(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);

}

}

输出量

2020-09-21

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值