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;
}
}