Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB IllegalAnnotationException is thrown during parsing XML

Tags:

java

xml

jaxb

This is my XML file:

<fields>     <field mappedField="Num">     </field>          <field mappedField="Type">           </field>     </fields> 

I created 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.

like image 723
WelcomeTo Avatar asked May 29 '12 08:05

WelcomeTo


2 Answers

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:

package forum10795793;  import java.util.*; import javax.xml.bind.annotation.*;  @XmlRootElement(name = "fields") @XmlAccessorType(XmlAccessType.FIELD) public class Fields {      @XmlElement(name = "field")     List<Field> fields = new ArrayList<Field>();      public List<Field> getFields() {         return fields;     }      public void setFields(List<Field> fields) {         this.fields = fields;     }  } 

Field.java:

package forum10795793;  import javax.xml.bind.annotation.*;  @XmlAccessorType(XmlAccessType.FIELD) public class Field {      @XmlAttribute(name = "mappedField")     String mappedField;      public String getMappedField() {         return mappedField;     }      public void setMappedField(String mappedField) {         this.mappedField = mappedField;     }  } 

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:

package forum10795793;  import java.util.*; import javax.xml.bind.annotation.*;  @XmlRootElement(name = "fields") public class Fields {      List<Field> fields = new ArrayList<Field>();      @XmlElement(name = "field")     public List<Field> getFields() {         return fields;     }      public void setFields(List<Field> fields) {         this.fields = fields;     }  } 

Field.java:

package forum10795793;  import javax.xml.bind.annotation.*;  public class Field {      String mappedField;      @XmlAttribute(name = "mappedField")     public String getMappedField() {         return mappedField;     }      public void setMappedField(String mappedField) {         this.mappedField = mappedField;     }  } 

For More Information

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
like image 116
bdoughan Avatar answered Sep 20 '22 09:09

bdoughan


I can't understand why this JAXB IllegalAnnotationException is thrown

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 the list variable which gives something like:

[org.mortbay.jetty.Handler is an interface, and JAXB can't handle interfaces. this problem is related to the following location:     at org.mortbay.jetty.Handler     at public org.mortbay.jetty.Handler[] org.mortbay.jetty.handler.HandlerCollection.getHandlers()     at org.mortbay.jetty.handler.HandlerCollection     at org.mortbay.jetty.handler.ContextHandlerCollection     at com.mprew.ec2.commons.server.LocalContextHandlerCollection     at private com.mprew.ec2.commons.server.LocalContextHandlerCollection com.mprew.ec2.commons.services.jaxws_asm.SetLocalContextHandlerCollection.arg0     at com.mprew.ec2.commons.services.jaxws_asm.SetLocalContextHandlerCollection, org.mortbay.jetty.Handler does not have a no-arg default constructor.] .... 

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 my LocalContextHandlerCollection. I removed a dependency loop and the error cleared.

Hopefully this will be helpful to others.

like image 37
Gray Avatar answered Sep 17 '22 09:09

Gray