问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/26527655/xmlelement-required-true-for-integer-doesnt-work