【发布时间】:2012-11-22 08:08:56
【问题描述】:
我正在使用 JAXB 2.1,但我对所看到的 XML 输出感到困惑。下面我有两个扩展同一个父类的子类。当使用 REST 在浏览器中编组和查看 XML 时,子类 1 (GeoLocationDecodedPayload) 始终如预期具有 geoLocationDecodedPayload 的根元素。由于某种原因,子类 2 (AltitudeDecodedPayload) 没有 将 altitudeDecodedPayload 作为其根元素,这在其 @XMLRootElement 注释中指定是意料之外的。 XML 输出显示 geoPayload 的超类 (GeoPayload) @XMLRootElement。任何想法为什么这两个类的行为不同?
子类 1:
package com.api.model.vo.decoder;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.api.util.decoder.DecoderConstants;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "geoLocationDecodedPayload")
public class GeoLocationDecodedPayload extends GeoPayload implements Serializable {
public GeoLocationDecodedPayload() {}
}
子类 2:
package com.api.model.vo.decoder;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.api.util.decoder.DecoderConstants;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "altitudeDecodedPayload")
public class AltitudeDecodedPayload extends GeoPayload implements Serializable {
public AltitudeDecodedPayload() {}
}
父类:
package com.api.model.vo.decoder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "geoPayload")
public class GeoPayload {
public GeoPayload() {}
}
【问题讨论】: