@XmlAttribute appears as @XmlElement
On my webservice I define a variable as @XmlAttribute
:
@XmlAttribute
protected String domain;
But when I make a query via SoapUi, it appears as an XML element:
<ns:domain>domain</ns:domain>
I can't find any mistake in my开发者_高级运维 code..
How do I fix this problem?
You show the annotation on the field, but JAXB uses property (getter/setter method) access by default. Have you changed JAXB's default access? Try putting the annotation on the getter method instead.
Edit: Since you seem to be having trouble, here's an executable example:
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; }
}
Output (formatted):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo anAttribute="my attribute value">
<anElement>my element value</anElement>
</foo>
I've reimplemented the project with the Axis 2 code generator. Now it works.
I don't know what was the mistake..
精彩评论