编辑:我最初遗漏了我的问题中可能重要的细节——返回要编码的 Java 对象的服务方法返回接口(interface)类型 ( Foo
) 而不是类实现类型( FooImpl
)。
我有一个简单的 Java 类,其中包含多个元素和属性的 JAX-B 注释:
@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;
}
}
编辑:FooImpl 类有一个名为 Foo
的接口(interface):
public interface Foo {
public String getName();
public void setName(final String name);
public String getId();
public void setId(final String id);
}
当我有一个返回 Foo
的服务方法时,我得到了我所期望的:
<foo id="abc123">
<name>bar</name>
</foo>
但我还有另一个包含 List<Foo>
的类当它被编码时, foo
的 XML 元素不包含他们的id
属性!!
<foos>
<foo>
<name>bar1</name>
</foo>
<foo>
<name>bar2</name>
</foo>
</foos>
保存列表的类如下所示:
@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;
}
}
我碰巧使用 MOXy 作为我的 JAX-B 实现,但我认为这并不重要。
最佳答案
注意:我是 EclipseLink JAXB (MOXy) 的负责人,也是 JAXB (JSR-222) 专家组的成员。
由于您有一个类型为接口(interface)的属性,因此您需要在 @XmlElement
注释上指定实现类型:
@XmlElement(name = "foo", type=FooImpl.class)
public List<Foo> getFoos() {
return foos;
}
下面是一个完整的示例:
食物
package forum9137171;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "foos")
public class Foos {
private List<Foo> foos;
@XmlElement(name = "foo", type=FooImpl.class)
public List<Foo> getFoos() {
return foos;
}
public void setFoos(List<Foo> foos) {
this.foos = foos;
}
}
福
package forum9137171;
public interface Foo {
public String getName();
public void setName(final String name);
public String getId();
public void setId(final String id);
}
FoomImpl
package forum9137171;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "foo")
public class FooImpl implements Foo {
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;
}
}
演示
package forum9137171;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foos.class);
System.out.println(jc);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum9137171/input.xml");
Foos foos = (Foos) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(foos, System.out);
}
}
输入/输出
org.eclipse.persistence.jaxb.JAXBContext@16a786
<?xml version="1.0" encoding="UTF-8"?>
<foos>
<foo id="abc123">
<name>bar1</name>
</foo>
<foo id="def456">
<name>bar2</name>
</foo>
</foos>
了解更多信息
关于java - JAX-B : Missing XML attributes on child elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9137171/