This is my XML file:
<fields>
<field mappedField="Num">
</field>
<field mappedField="Type">
</field>
</fields>
I made 2 classes to parse it (Fields.java and Field.java):
@XmlRootElement(name = "fields")
public class Fields {
@XmlElement(name = "field")
List<Field> fields = new ArrayList<Field>();
//getter, setter
}
and
public class Field {
@XmlAttribute(name = "mappedField")
String mappedField;
/getter,setter
}
But I get this exception.
[INFO] com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
[INFO] at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:66) ~[na:1.6.0_07]
[INFO] at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:422) ~[na:1.6.0_07]
[INFO] at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:270) ~[na:1.6.0_07]
I can't understand why this exception rises. Exception is here:
JAXBContext context = JAXBContext.newInstance(Fields.class);
I use JDK 1.6_0.0.7. Thanks.
This is because, by default, Jaxb when serializes a pojo, looks for the annotations over the public members(getters or setters) of the properties. But, you are providing annotations on fields. so, either change and set the annotations on setters or getters of properties, or sets the XmlAccessortype to field.
Option 1::
Option 2::
For more detail and depth, check the following JDK documentation http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlAccessorType.html
I had this same issue, I was passing a spring bean back as a ResponseBody object. When I handed back an object created by new, all was good.