【问题标题】:JAXB Annotated Child Class Using Super Classes XMLRootElement Name Value使用超类 XMLRootElement 名称值的 JAXB 注释子类
【发布时间】: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() {}
}

【问题讨论】:

    标签: java jaxb


    【解决方案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) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 2013-07-05
      • 2010-10-26
      相关资源
      最近更新 更多