开发者

JAXB marshalling superclass

开发者 https://www.devze.com 2022-12-22 07:55 出处:网络
I am writing a Resteasy server application and am having trouble getting my superclasses to marshal. I have code something like this:

I am writing a Resteasy server application and am having trouble getting my superclasses to marshal. I have code something like this:

@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; }
}

When I marshal the Employee class to XML开发者_运维问答, I get something like this:

<employee>
  <id>12345</id>
</employee>

with no output of the name field inherited from the Person class.

What am I doing wrong?

Thanks, Ralph


I'm not sure how you're configuring the JAXB context or marshaller but the following:-

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);

}

gives:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <name>Ralph</name>
    <id>1</id>
</employee>
0

精彩评论

暂无评论...
验证码 换一张
取 消