java JAXB 是否总是忽略@XmlType/propOrder 中未指定的“额外”元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11104697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:48:21  来源:igfitidea点击:

Does JAXB always ignore 'extra' elements not specified in @XmlType/propOrder?

javaxmljaxb

提问by Derek

If I have a class annotated with

如果我有一个类注释

@XmlType(name = "someDTO", propOrder = { "firstField", "secondField", })

@XmlType(name = "someDTO", propOrder = { "firstField", "secondField", })

but the XML (from a SOAP response, say) looks like

但是 XML(来自 SOAP 响应,比如说)看起来像

<return><firstField>a</firstField><secondField>b</secondField><thirdField>c</thirdField></return>

<return><firstField>a</firstField><secondField>b</secondField><thirdField>c</thirdField></return>

My object will still get firstField and secondField populated, and thirdField is ignored.

我的对象仍然会填充 firstField 和 secondField,而thirdField 被忽略。

Why is this? Will this always be the case? Is there a way to prevent object creation if extra fields are present?

为什么是这样?会一直这样吗?如果存在额外的字段,有没有办法阻止对象创建?

回答by bdoughan

Some JAXB (JSR-222)implementations will complain if there are properties mapped to XML elements that are not included in the propOrder. propOderon @XmlTypeis not used to control which elements are included/excluded.

一些JAXB(JSR-222) ,如果有未包含在映射到XML元素的属性的实现会抱怨propOrderpropOderon@XmlType不用于控制包含/排除哪些元素。

Options for Excluding Properties

排除属性的选项

  1. If you want to exclude less than half of the properties then I would suggest marking the ones you wish to exclude with @XmlTransient.
  2. 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.
  1. 如果您想排除少于一半的属性,那么我建议您用 标记您希望排除的属性@XmlTransient
  2. 如果您希望排除一半以上的属性,那么我建议您使用@XmlAccessorType(XmlAccessType.NONE)并注释您希望包含的属性。

For More Information

想要查询更多的信息