3

我正在使用 JAXB 2.1,我对看到的 XML 输出感到困惑。下面我有两个扩展同一个父类的子类。当使用 REST 在浏览器中编组和查看 XML 时,子类 1 (GeoLocationDecodedPayload) 始终具有geoLocationDecodedPayload的根元素,如预期的那样。由于某种原因,子类 2 (AltitudeDecodedPayload)没有作为其根元素的heightDecodedPayload,这在其 @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() {}
}
4

1 回答 1

1

我忘了在下面包含 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) 
于 2012-11-22T18:02:05.270 回答