Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

xml

jaxb

If I have a class annotated with

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

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

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

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?

like image 215
Derek Avatar asked Jun 19 '12 15:06

Derek


1 Answers

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.

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.

For More Information

  • http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html
like image 114
bdoughan Avatar answered Nov 15 '22 14:11

bdoughan