我正在编写一个Resteasy服务器应用程序,但我很难让我的超级课程编组。我有这样的代码:
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "person")
class Person {
protected String name;
@XmlElement(name = "name")
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "employee")
class Employee extends Person {
protected Integer id;
@XmlElement(name = "id")
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
}
当我将Employee类编组为XML时,我得到这样的结果:
<employee>
<id>12345</id>
</employee>
没有继承自Person类的名称字段的输出。
我做错了什么?
谢谢,拉尔夫
答案 0 :(得分:0)
我不确定您是如何配置JAXB上下文或marshaller的,但以下内容: -
public static void main(String[] args) throws Exception
{
Employee employee = new Employee();
employee.setId(1);
employee.setName("Ralph");
JAXBContext context = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(employee, System.out);
}
得到: -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<name>Ralph</name>
<id>1</id>
</employee>