I came across a situation where JAXB refuses an unmarshal XML element if the corresponding Java field does not have a namespace annotation. This behavior only starts in JDK 1.8.0_111 (or, possibly, in 102). Earlier versions of JDK 1.8 work.
Test case:
Java class (abbreviated):
package my.package; @XmlRootElement(name = "MyElement", namespace="myns") public class MyElement { @XmlElement(name = "subEl") private String subEl; }
XML:
<MyElement xmlns="myns"> <subEl>text1</subEl> </MyElement>
package-info.java:
@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED) package my.package;
Unmarshalling code:
JAXBContext jc = JAXBContext.newInstance(MyElement.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); MyElement myel = (MyElement) unmarshaller.unmarshal(xmlStream); System.out.println("Parse result: "+ myel);
With JDK 1.8.0_101 (and earlier), this prints:
Analysis Result: MyElement [subEl = subEl]
With JDK 1.8.0_111 I get:
Analysis Result: MyElement [subEl = null]
So, JDK 1.8.0_111 discards the unmarshal element "MyElement".
If I specify the namespace in the field annotation:
@XmlElement(name = "subEl", namespace="myns") private String subEl;
It works as expected in all versions of the JDK.
What's going on here?
As far as I understand, the parameter elementFormDefault = XmlNsForm.QUALIFIED should cause all fields of the MyElement class to "inherit" the class namespace. Javadocs for @XmlElement say:
If the value is "## default", then the namespace is defined as follows:
If the enclosed package contains the XmlSchema annotation and its elementFormDefault QUALIFIED, then the enclosed class namespace. Otherwise '' (which creates an unqualified element by default Namespace.
Default: "## default"
So why does JDK 1.8.0_111 not cancel the element?
Note: JAXB error report # 1087 - Unmarshalling Wrapped items with elementsFor element_Name = qualified failure (it seems JAXB-1087- Unmarshalling Wrapped items with elementForName = qualified failed ), it seems to report the same problem - there is no answer yet.