JAXB使用超类XMLRootElement名称值注释的子类



我使用的是JAXB 2.1,看到的XML输出让我感到困惑。下面我有两个子类,它们扩展了同一个父类。在使用REST的浏览器中以XML形式进行编组和查看时,子类1(GeoLocationDecodedPPayload)始终具有一个根元素geoLocationDecidedPPayload,正如预期的那样。由于某些原因,子类2(AltitudeDecodedPPayload)没有altitudeDecode Payload作为其根元素,这在其@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() {}
}

我忘了在下面包含AltitudeDecodedPayload.class。这解决了我的问题。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="payloadResponse")
public class PayloadResponse extends AbstractResponse{
@XmlElementWrapper(name="decodedPayloads")
@XmlElementRefs({
@XmlElementRef(type=GeoPayload.class),
@XmlElementRef(type=GeoLocationDecodedPayload .class),
@XmlElementRef(type=AltitudeDecodedPayload .class) 

最新更新