JAXB是否始终忽略@ XmlType / propOrder中未指定的“额外”元素? [英] Does JAXB always ignore 'extra' elements not specified in @XmlType/propOrder?
问题描述
如果我有一个注释的类
@XmlType(name =someDTO,propOrder = {
firstField ,
secondField,
})
但XML(来自SOAP响应,比方说)看起来像
but the XML (from a SOAP response, say) looks like
< return>< firstField> a< / firstField>< secondField> b< / secondField>< thirdField> c< / thirdField>< / return>
我的对象仍将填充firstField和secondField,并忽略thirdField。
My object will still get firstField and secondField populated, and thirdField is ignored.
这是为什么?这总是如此吗?如果存在额外的字段,是否有办法防止对象创建?
Why is this? Will this always be the case? Is there a way to prevent object creation if extra fields are present?
推荐答案
一些 JAXB(JSR-222) 实现会抱怨如果有属性映射到非元素的XML元素包含在 propOrder
中。 propOder
on @XmlType
不用于控制包含/排除哪些元素。
Some JAXB (JSR-222) implementations will complain if there are properties mapped to XML elements that are not included in the propOrder
. propOder
on @XmlType
is not used to control which elements are included/excluded.
排除物业的选项
- 如果你想要排除不到一半的属性,那么我建议用
@XmlTransient
标记要排除的属性。 - 如果你希望排除超过一半的属性然后我建议使用
@XmlAccessorType(XmlAccessType.NONE)
并注释您希望包含的属性。
- If you want to exclude less than half of the properties then I would suggest marking the ones you wish to exclude with
@XmlTransient
. - If you wish to exclude more than half of the properties then I would suggest using
@XmlAccessorType(XmlAccessType.NONE)
and annotating the properties you wish to include.
更多信息
- http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html
这篇关于JAXB是否始终忽略@ XmlType / propOrder中未指定的“额外”元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!