簡體   English   中英

JAXB編組和繼承

[英]JAXB marshalling and inheritance

我有2個班,一個擴展另一個班。 超類編組正確,但是添加一個屬性的子類卻沒有。 XML中不存在額外屬性。

超類:

@XmlRootElement()
@XmlAccessorType(XmlAccessType.NONE)
public class SessionRecord extends Record {

  SimpleDateFormat hhmm = new SimpleDateFormat("HH:mm");
  SimpleDateFormat day = new SimpleDateFormat("EEEEE");

  @XmlAttribute protected int sessionId;
  @XmlAttribute protected boolean open;
  @XmlAttribute protected boolean night;
  protected Date start;
  protected Date finish;
  protected boolean setup;
  protected boolean takedown;

  @XmlAttribute
  public String getDescription() {
        if (start==null) start = new Date();
        if (finish==null) finish = new Date();
        return day.format(start)+(night ? " Night " : " ")+hhmm.format(start)+"-"+hhmm.format(finish)+" "+type();
  }

  private String type() {
        return setup ? "Setup" : (open ? "Open" : (takedown ? "Takedown" : ""));
  }

  @XmlAttribute
  public boolean isSetupTakedown() {
        return setup || takedown;
  }

}

這會生成類似於以下內容的XML元素:

<sessionRecord setupTakedown="true" description="Saturday 09:00-13:00 Setup" night="false" open="false" sessionId="0"/>

這沒關系。

但是子類:

@XmlRootElement()
public class VolunteerSession extends SessionRecord {

  @XmlAttribute private boolean available;

}

生成相同的輸出, available屬性未編組。 為什么JAXB沒有編組額外的屬性?

編輯

更多信息:

記錄超類僅僅是這樣的:

public abstract class Record {}

這是表示頂級文檔元素的類。 它包含記錄列表:

@XmlRootElement(name="response")
@XmlSeeAlso({
  RecordList.class, 
  VolunteerAssignment.class, 
  VolunteerRecord.class,
  SessionRecord.class,
  VolunteerSession.class,
  VolunteerArea.class,
  PossibleAssignment.class})
public class XMLResponse {

  @XmlAttribute private String errorMessage;

  private List<RecordList<? extends Record>> recordLists  = new ArrayList<RecordList<? extends Record>>();

  //snip...

  public void setError(String errorMessage) {
        this.errorMessage = errorMessage;
  }

  @XmlMixed
  public List<RecordList<? extends Record>> getRecordLists() {
        return recordLists;
  }

}

最后,RecordList

@XmlRootElement()
public class RecordList<T extends Record> {

  @XmlAttribute private String name;
  @XmlAttribute private int total;
  @XmlAttribute private int count;
  @XmlAttribute private int start;
  @XmlAttribute private boolean update;
  private List<T> records;

  // snip constructors, setters

  @XmlMixed
  public List<T> getRecords() {
        return records;
  }

}

聽起來好像VolunteerSession類沒有被包含在JAXBContext 這可能會發生,具體取決於您創建JAXBContext 下面是一些示例代碼,其中基於3個不同的JAXBContext實例編組相同的對象,每個實例都從不同的類引導。

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        VolunteerSession volunteerSession = new VolunteerSession();

        marshal(VolunteerSession.class, volunteerSession);
        marshal(SessionRecord.class, volunteerSession);
        marshal(XMLResponse.class, volunteerSession);
    }

    private static void marshal(Class bootstrapClass, Object object) throws Exception {
        System.out.println(bootstrapClass.getName());
        JAXBContext jc = JAXBContext.newInstance(bootstrapClass);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, System.out);
        System.out.println();
    }

}

產量

  1. JAXBContextVolunteerSession引導時,顯然它具有必要的信息。
  2. JAXBContext從超類SessionRecord引導時,它不會SessionRecord VolunteerSession JAXB將自動處理超類的元數據,但不會處理子類。 在這種情況下, @XmlSeeAlso通常用於引用映射的子類。
  3. VolunteerRecord包含一個引用VolunteerSession@XmlSeeAlso注釋。 因此, VolunteerSession作為JAXBContext一部分進行處理,並在編組時包含必要的信息。
forum20908213.VolunteerSession
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<volunteerSession available="false" sessionId="0" open="false" night="false" description="Sunday 05:53-05:53 " setupTakedown="false"/>

forum20908213.SessionRecord
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sessionRecord sessionId="0" open="false" night="false" description="Sunday 05:53-05:53 " setupTakedown="false"/>

forum20908213.XMLResponse
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<volunteerSession available="false" sessionId="0" open="false" night="false" description="Sunday 05:53-05:53 " setupTakedown="false"/>

您必須在父類的@XmlSeeAlso注釋中列出所有子類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM