XmlElement required = true for Integer doesn't work

こ雲淡風輕ζ 提交于 2019-12-11 10:27:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!