JAXB - Data Binding Exception
Yesterday, while attempting to convert a XML string into a POJO, I encountered this error:
javax.xml.bind.DataBindingException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "entries"
this problem is related to the following location:
at ....
this problem is related to the following location:
at ....
at ....
etc...
Here's my POJO codes:
@XmlRootElement
public class EntryMappings {
@XmlElement(name="entryMapping")
private List<EntryMapping> entries;
public List<EntryMapping> getEntries() {
return this.entries;
}
}
Turns out that JAXB default binding access type, XmlAccessType.PUBLIC_MEMBER, will attempt to bind all public fields, annotated fields and properties. In my case, it attempted the binding on both the entries field and the getEntries() field.
The solution is to pick the XmlAccessType such that the binding will happen only once.
Example 1 - Bind by fields:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class EntryMappings {
@XmlElement(name="entryMapping")
private List<EntryMapping> entries;
public List<EntryMapping> getEntries() {
return this.entries;
}
}
Example 2 - Bind by properties:
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class EntryMappings {
private List<EntryMapping> entries;
@XmlElement(name="entryMapping")
public List<EntryMapping> getEntries() {
return this.entries;
}
}
Example 3 - Bind by annotated fields or annotated properties
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class EntryMappings {
@XmlElement(name="entryMapping")
private List<EntryMapping> entries;
public List<EntryMapping> getEntries() {
return this.entries;
}
}
credits and references:
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
http://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlAccessType.html
Written by Ivan Sim
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ivanhcsim
Authors
ihcsim
751.3K
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#