Reputation: 202
I need a little help with JAXB annotations. I have one XML that I want to get as a java object. If I will run it as it is I will get an error about the properties with the same name (see below). When I will comment out setters for lists in Feature and Scenario classes the error will disappear, but I will need those setters...
Error:
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "scenarioList"
this problem is related to the following location:
at public java.util.ArrayList generator.model.Feature.getScenarioList()
at generator.model.Feature
at private java.util.ArrayList generator.model.Features.featureList
at generator.model.Features
this problem is related to the following location:
at private java.util.ArrayList generator.model.Feature.scenarioList
at generator.model.Feature
at private java.util.ArrayList generator.model.Features.featureList
at generator.model.Features
Class has two properties of the same name "stepList"
this problem is related to the following location:
at public java.util.ArrayList generator.model.Scenario.getStepList()
at generator.model.Scenario
at private java.util.ArrayList generator.model.Feature.scenarioList
at generator.model.Feature
at private java.util.ArrayList generator.model.Features.featureList
at generator.model.Features
this problem is related to the following location:
at private java.util.ArrayList generator.model.Scenario.stepList
at generator.model.Scenario
at private java.util.ArrayList generator.model.Feature.scenarioList
at generator.model.Feature
at private java.util.ArrayList generator.model.Features.featureList
at generator.model.Features
XML to parse:
<?xml version="1.0" encoding="UTF-8"?>
<features>
<feature id="1">
<name>Feature name 1</name>
<description>Feature description 1</description>
<scenarios>
<scenario id="1">
<name>Scenario name 1</name>
<steps>
<step id="1"></step>
<step id="2"></step>
</steps>
</scenario>
<scenario id="2">
<name>Scenario name 2</name>
<steps>
<step id="1"></step>
<step id="2"></step>
</steps>
</scenario>
</scenarios>
</feature>
<feature id="2">
<name>Feature name 2</name>
<description>Feature description 2</description>
<scenarios>
<scenario id="4">
<name>Scenario name 1</name>
<steps>
<step id="1"></step>
</steps>
</scenario>
</scenarios>
</feature>
</features>
Here are the model classes:
Features.java
package generator.model;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "features")
public class Features {
@XmlElement(name = "feature")
private ArrayList<Feature> featureList = null;
public void setFeaturesList(ArrayList<Feature> featureList) {
this.featureList = featureList;
}
public ArrayList<Feature> getFeatureList() {
return featureList;
}
}
Feature.java
package generator.model;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "feature")
public class Feature {
@XmlElementWrapper(name = "scenarios")
@XmlElement(name= "scenario")
private ArrayList<Scenario> scenarioList = null;
private int id;
private String name;
private String description;
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ArrayList<Scenario> getScenarioList() {
return scenarioList;
}
public void setScenarioList(ArrayList<Scenario> scenarioList) {
this.scenarioList = scenarioList;
}
}
Scenario.java
package generator.model;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "scenario")
public class Scenario {
@XmlElementWrapper(name = "steps")
@XmlElement(name= "step")
private ArrayList<Step> stepList = null;
private int id;
private String name;
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Step> getStepList() {
return stepList;
}
public void setStepList(ArrayList<Step> stepList) {
this.stepList = stepList;
}
}
Step.java
package generator.model;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "step")
public class Step {
private int id;
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Upvotes: 0
Views: 607
Reputation: 2947
JAXB automatically maps fields or properties. If the @XmlAccessorType
is absent, then the default is XmlAccessType.PUBLIC_MEMBER
, that means (see javadoc)
Every public getter/setter pair and every public field will be automatically bound to XML, unless annotated by XmlTransient.
In your case, by default, all public
getter/setter are mapped even if no annotation is present. Fields are not mapped, because they are private
, unless explicitly annotated.
So, there is a conflict with stepList
and scenarioList
that are mapped two times: The public
getter/setter, because @XmlAccessorType
is absent, and the field, because it is annotated.
And note that in your Features class, you will have a <feature>
element (annotated private featureList field), and a <featureList>
element (public getter for featureList), both with the same content.
You could avoid it, adding an @XmlTransient
annotation to the property getter to avoid the automatic mapping. But I would suggest choosing to map either fields or properties, and specify the @XmlAccessorType accordingly.
For example, your Scenario.java could be mapped with:
// Non static, non transient fields will be automatically be mapped unless annotated with XmlTransient.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "scenario")
public class Scenario {
@XmlElementWrapper(name = "steps")
@XmlElement(name= "step")
private ArrayList<Step> stepList = null;
@XmlAttribute
private int id;
// Mapping a field to an element is the default, so this annotation is not strictly
// needed, unless you want to change the default element name
@XmlElement
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Step> getStepList() {
return stepList;
}
public void setStepList(ArrayList<Step> stepList) {
this.stepList = stepList;
}
}
Upvotes: 1