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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…