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.
I also was getting the
### counts of IllegalAnnotationExceptions
exception and it seemed to be due to an improper dependency hierarchy in my Spring wiring.I figured it out by putting a breakpoint in the JAXB code when it does the throw. For me this was at
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check()
. Then I dumped thelist
variable which gives something like:The
does not have a no-arg default constructor
seemed to me to be misleading. Maybe I wasn't understanding what the exception was saying. But it did indicate that there was a problem with myLocalContextHandlerCollection
. I removed a dependency loop and the error cleared.Hopefully this will be helpful to others.
I once received this message after thinking that putting
@XmlTransient
on a field I didn't need to serialize, in a class that was annotated with@XmlAccessorType(XmlAccessType.NONE)
.In that case, removing
XmlTransient
resolved the issue. I am not a JAXB expert, but I suspect that because AccessType.NONE indicates that no auto-serialization should be done (i.e. fields must be specifically annotated to serialize them) that makesXmlTransient
illegal since its sole purpose is to exclude a field from auto-serialization.One of the following may cause the exception:
The exception is due to your JAXB (JSR-222) implementation believing that there are two things mapped with the same name (a field and a property). There are a couple of options for your use case:
OPTION #1 - Annotate the Field with
@XmlAccessorType(XmlAccessType.FIELD)
If you want to annotation the field then you should specify
@XmlAccessorType(XmlAccessType.FIELD)
Fields.java:
Field.java:
OPTION #2 - Annotate the Properties
The default accessor type is
XmlAccessType.PUBLIC
. This means that by default JAXB implementations will map public fields and accessors to XML. Using the default setting you should annotate the public accessors where you want to override the default mapping behaviour.Fields.java:
Field.java:
For More Information
All below options worked for me.
Option 1: Annotation for FIELD at class & field level with getter/setter methods
Option 2: No Annotation for FIELD at class level(@XmlAccessorType(XmlAccessType.FIELD) and with only getter method. Adding Setter method will throw the error as we are not including the Annotation in this case. Remember, setter is not required when you explicitly set the values in your XML file.
Option 3: Annotation at getter method alone. Remember we can also use the setter method here as we are not doing any FIELD level annotation in this case.
Hope this helps you!
I was having the same issue
My Issue
Changed made to solve it are below
I change the respective name to namespace
why?
As written in error log two classes have same name so we should use namespace because XML namespaces are used for providing uniquely named elements and attributes in an XML document.