java jaxb注解xmlnull,JAXB Marshaller没有值为null的元素

When I marshall a java object using JAXB Marshaller, the marshaller does not create empty elements for null files in the java object. For example, I have a following java object:

public class PersonTraining {

@XmlElement(name = "Val1", required = true)

protected BigDecimal val1;

@XmlElement(name = "Val2", required = true, nillable = true)

protected BigDecimal val2;

@XmlElement(name = "Val3", required = true, nillable = true)

@XmlSchemaType(name = "dateTime")

protected XMLGregorianCalendar val3;

}

When I take an instance of this object, and marshall into an XML, I get the following (This is beacuse I did not set the value for Val2):

1

2010-01-01T00:00:00.0-05:00

However, I had expected hte following result from the marshalling operation (Infact, I specifically need element as well so that the XML can be validated against the XSD)

1

2010-01-01T00:00:00.0-05:00

Please let me know what option I would need to set so that the null value in the object attributes can ALSO be marshalled, and returned as empty/null elements.

Here is the marshalling code:

StringWriter sw = new StringWriter();

JAXBContext jc = JAXBContext.newInstance("person_training");

Marshaller m = jc.createMarshaller();

m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

m.marshal(ptl, sw);

解决方案

By default a JAXB (JSR-222) implementation will not marshal an attribute/element for null values. This will be true for the following field in your Java model.

@XmlElement(name = "Val1", required = true)

protected BigDecimal val1;

You can override this behaviour by specifying nillable=true on the @XmlElement annotation like you have done here:

@XmlElement(name = "Val2", required = true, nillable = true)

protected BigDecimal val2;

This will cause the xsi:nil="true" attribute to be leverage:

For more information:

Java Model

PersonTraining

Since you are annotating the fields you should make sure you specify @XmlAccessorType(XmlAccessType.FIELD) at the class or package level (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).

import java.math.BigDecimal;

import javax.xml.bind.annotation.*;

import javax.xml.datatype.XMLGregorianCalendar;

@XmlRootElement

@XmlAccessorType(XmlAccessType.FIELD)

public class PersonTraining {

@XmlElement(name = "Val1", required = true)

protected BigDecimal val1;

@XmlElement(name = "Val2", required = true, nillable = true)

protected BigDecimal val2;

@XmlElement(name = "Val3", required = true, nillable = true)

@XmlSchemaType(name = "dateTime")

protected XMLGregorianCalendar val3;

}

Demo Code

Demo

import javax.xml.bind.*;

public class Demo {

public static void main(String[] args) throws Exception {

JAXBContext jc = JAXBContext.newInstance(PersonTraining.class);

PersonTraining pt = new PersonTraining();

Marshaller marshaller = jc.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(pt, System.out);

}

}

Output

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值