简体   繁体   中英

XmlElement required = true for Integer doesn't work

I use jax-rs service. I have two fields in my class annotated with @XmlElement(required = true), one for Boolean, another for Integer. but if I send xml tags without value, for boolean I receive null, but for Integer I receive 0

@XmlElement(required = true)
private Integer intValue;

@XmlElement(required = true)
private Boolean booleanValue;

why I'm not receive null in Integer?

I just should added defaultValue = "null"

@XmlElement(required = true, defaultValue = "null")
private Integer intValue;

There are 'interesting' (not expected) behavior of Integer fields. I can not find an explanation for what values ​​are assigned to the default Integers in jaxb.

The required property on the @XmlElement annotation has no impact on the marshalling/unmarshalling performed by JAXB. It simply used during XML Schema generation to determine if the element declaration should have a minOccurs of 0 (for false), or 1 (for true).

The difference between unmarshalling behaviour for Boolean and Integer is due to a bug in the JAXB reference implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM