Edit: I originally left out what may be an important detail from my question — My service methods that return Java objects to be marshaled return an interface type (Foo) rather than the class implementation type (FooImpl).
I have a simple Java class with JAX-B annotations for several elements and attributes:
@XmlRootElement(name = "foo")
public class FooImpl {
private String id;
private String name;
@XmlElement
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@XmlAttribute
public String getId() {
return Id;
}
public void setId(final String id) {
this.id = id;
}
}
Edit: The FooImpl class has an interface named Foo:
public interface Foo {
public String getName();
public void setName(final String name);
public String getId();
public void setId(final String id);
}
When I have a service method that returns a Foo, I get what I expect:
<foo id="abc123">
<name>bar</name>
</foo>
But I also have another class that contains a List<Foo> and when it is marshalled, the XML elements for foo do not contain their id attribute!!
<foos>
<foo>
<name>bar1</name>
</foo>
<foo>
<name>bar2</name>
</foo>
</foos>
The class that holds the list looks like this:
@XmlRootElement(name = "foos")
public class Foos {
private List<Foo> foos;
@XmlElement(name = "foo")
public List<Foo> getFoos() {
return foos;
}
public void setFoos(List<Foo> foos) {
this.foos = foos;
}
}
I happen to be using MOXy as my JAX-B implementation, but I do not think that matters.
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Since you have a property whose type is an interface you will need to specify the implementing type on the
@XmlElementannotation:Below is a complete example:
Foos
Foo
FoomImpl
Demo
Input/Output
For More Information