简体   繁体   English

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

[英]JAXB Annotated Child Class Using Super Classes XMLRootElement Name Value

I'm using JAXB 2.1 and I'm confused by the XML output I'm seeing.我正在使用 JAXB 2.1,但我对所看到的 XML 输出感到困惑。 Below I have two child classes that extend the same parent.下面我有两个扩展同一个父类的子类。 When marshalled and viewed as XML in a browser using REST, Child class 1 (GeoLocationDecodedPayload) always has a root element of geoLocationDecodedPayload as expected.当使用 REST 在浏览器中以 XML 格式编组和查看时,子类 1 (GeoLocationDecodedPayload) 始终如预期一样具有geoLocationDecodedPayload的根元素。 For some reason child class 2 (AltitudeDecodedPayload) doesn't have altitudeDecodedPayload as its root element which is unexpected as its specified in its @XMLRootElement annotation.出于某种原因,子类2(AltitudeDecodedPayload)具有altitudeDecodedPayload作为其根元素作为其在其@XmlRootElement注释指定这是出乎意料的。 The XML output shows the super class (GeoPayload) @XMLRootElement of geoPayload . XML 输出显示了 geoPayload 的超类 (GeoPayload) @XMLRootElement Any ideas why these two class act differently?任何想法为什么这两个类的行为不同?

child class 1:儿童班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() {}

}

child class 2:儿童班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() {}

}

parent class:父类:

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() {}
}

I had forgot to include AltitudeDecodedPayload.class in the below.我忘了在下面包含 AltitudeDecodedPayload.class。 This fixed my issue.这解决了我的问题。

@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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM