JAXB Marshalling Superclass.
-
19-09-2019 - |
سؤال
أنا أكتب تطبيق خادم Resteasy وأوجه مشكلة في الحصول على Superclasses إلى Marshal. لدي رمز شيء مثل هذا:
@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; }
}
عندما أسهب فئة الموظف إلى XML، أحصل على شيء من هذا القبيل:
<employee>
<id>12345</id>
</employee>
مع عدم وجود إخراج من حقل الاسم الموروث من فئة الشخص.
ما الخطأ الذي افعله؟
شكرا يا رالف
المحلول
لست متأكدا من كيفية تكوين سياق 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>
لا تنتمي إلى StackOverflow