bonjour,

Je suis nouvelle dans la technologie JAXB. Et je n'arrive pas a saisir comment résoudre l'exception suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "data"
Si j'enlève l'annotatoin "@XMlElement(required = true)" au dessu de mon variable data je n'ai pas le problème mais j'aimerai la garder pour visibilité du code.
Voici mes classes :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
public class Main {
    public static void main(String[] args) {
        final RootElement rootElement = new RootElement();
        rootElement.getElement().getSubElements().add(new SubElement("Coucou"));
        rootElement.getElement().getSubElements().add(new SubElement("tout le monde"));
        Writer writer = null;
        try {
            final JAXBContext context = JAXBContext.newInstance(RootElement.class);
            final Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            // Console view
            marshaller.marshal(rootElement, System.out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class RootElement {
    @XmlElement(required = true)
    private Element e;
 
    public RootElement() {
        e = new Element();
    }
 
    public Element getElement() {
        return e;
    }
 
    public void setElement(final Element e) {
        this.e = e;
    }
}
 
public class Element {
    @XmlElement(required = true)
    private List<SubElement> subElements;
 
    public Element() {
        subElements = new ArrayList<SubElement>();
    }
 
    public List<SubElement> getSubElements() {
        return subElements;
    }
 
    public void setSubElements(final List<SubElement> e) {
        this.subelements = e;
    }
}
 
public class SubElement {
 
    @XmlElement(required = true)
    private String data;
 
    public SubElement() {
        this.data = "";
    }
 
    public SubElements(final String data) {
        this.data = data;
    }
 
    public String getData() {
        return data;
    }
 
    public void setData(final String data) {
        this.data = data;
    }
}
Merci pour votre aide.