JAXB 编组超类

发布于 2024-08-24 10:46:05 字数 766 浏览 9 评论 0原文

我正在编写 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 类继承的 name 字段的输出。

我做错了什么?

谢谢,拉尔夫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜点 2024-08-31 10:46:05

我不确定您如何配置 JAXB 上下文或编组器,但以下内容:-

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文