2011-08-25 3 views
0

Sur mon webservice je définis une variable comme @XmlAttribute:@XmlAttribute apparaît comme @XmlElement

@XmlAttribute 
protected String domain; 

Mais quand je fais une requête via SoapUI, il apparaît comme un élément XML:

<ns:domain>domain</ns:domain> 

I ne trouve pas d'erreur dans mon code ..

Comment résoudre ce problème?

Répondre

1

J'ai remis sur pied le projet avec le Axis 2 générateur de code. Maintenant ça marche.

Je ne sais pas quelle était l'erreur ..

1

Vous affichez l'annotation sur le champ, mais JAXB utilise l'accès par propriété (méthode getter/setter) par défaut. Avez-vous changé l'accès par défaut de JAXB? Essayez de mettre l'annotation sur la méthode getter à la place.

Edit: Puisque vous semblez avoir du mal, voici un exemple exécutable:

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.annotation.*; 
import java.io.StringWriter; 

public static void main(String[] args) throws Exception { 
    Foo foo = new Foo("my attribute value", "my element value"); 
    Marshaller marshaller = JAXBContext.newInstance(Foo.class).createMarshaller(); 
    StringWriter stringWriter = new StringWriter(); 
    marshaller.marshal(foo, stringWriter); 
    System.out.println(stringWriter); 
} 

@XmlRootElement 
static class Foo { 
    private String anAttribute; 
    private String anElement; 

    Foo() {} 

    public Foo(String anAttribute, String anElement) { 
     this.anAttribute = anAttribute; 
     this.anElement = anElement; 
    } 

    @XmlAttribute 
    public String getAnAttribute() { return anAttribute; } 
    @XmlElement 
    public String getAnElement() { return anElement; } 
} 

sortie (formaté):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<foo anAttribute="my attribute value"> 
    <anElement>my element value</anElement> 
</foo> 
+0

Je l'ai essayé comme ceci: @XmlAttribute public String getDomain() { domaine de retour; } ne fonctionne pas – Starbax

+1

Mise à jour ma réponse avec un exemple de travail. –

+0

J'ai mis à jour mon message, il y a plus de code maintenant – Starbax

Questions connexes