문제

ReseSy Server 응용 프로그램을 작성하고 있으며 수퍼 클래스를 마샬링하는 데 어려움이 있습니다. 나는 다음과 같은 코드가 있습니다.

@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 컨텍스트 또는 마샬러를 어떻게 구성하고 있는지 잘 모르겠지만 다음은 다음과 같습니다.

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top