Вопрос аннотации JAXB - PullRequest
       4

Вопрос аннотации JAXB

0 голосов
/ 12 апреля 2020

Мне нужна небольшая помощь с аннотациями JAXB. У меня есть один XML, который я хочу получить как java объект. Если я буду запускать его как есть, я получу ошибку о свойствах с тем же именем (см. Ниже). Когда я закомментирую установщики для списков в классах объектов и сценариев, ошибка исчезнет, ​​но мне понадобятся эти установщики ...

Ошибка:

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 для анализа:

<?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>

Вот классы моделей:

Особенности. 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;
    }

}  

Сценарий. 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;  
    } 

}  

1 Ответ

1 голос
/ 12 апреля 2020

JAXB автоматически сопоставляет поля или свойства. Если @XmlAccessorType отсутствует, то по умолчанию используется значение XmlAccessType.PUBLIC_MEMBER, что означает ( см. Javado c)

Каждая опубликованная пара c геттер / установщик и каждое публикуемое поле c будет автоматически привязано к XML, если оно не аннотировано XmlTransient.

В вашем случае по умолчанию все public получатель / установщик отображаются, даже если аннотации нет подарок. Поля не отображаются, потому что они private, если явно не указано иное.

Итак, существует конфликт с stepList и scenarioList, которые отображаются два раза: public getter / setter, потому что @XmlAccessorType отсутствует, а поле, потому что оно аннотировано.

И обратите внимание, что в вашем классе Features у вас будет элемент <feature> (аннотированное частное поле featureList) и <featureList> element (publi c getter для featureList), оба с одинаковым содержимым.

Вы можете избежать этого, добавив аннотацию @XmlTransient в getter свойства, чтобы избежать автоматического сопоставления c. Но я бы предложил выбрать отображение полей или свойств и соответственно указать @XmlAccessorType.

Например, ваш сценарий. java может быть сопоставлен с:

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

}  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...