Java 使用超类XMLRootElement名称值的JAXB注释子类
我使用的是JAXB2.1,我对看到的XML输出感到困惑。下面我有两个子类,它们扩展了同一个父类。当在浏览器中使用REST进行编组并作为XML查看时,子类1(GeoLocationDecodedPayload)始终按预期具有GeoLocationDecodedPayload的根元素。由于某些原因,子类2(AltitudeDecodedPayload)没有将AltitudeDecodedPayload作为其根元素,这是在其@XMLRootElement注释中指定的意外情况。XML输出显示GeoPayload的超类(GeoPayload)@XMLRootElement。你知道为什么这两类人的行为不同吗 子类1:Java 使用超类XMLRootElement名称值的JAXB注释子类,java,jaxb,Java,Jaxb,我使用的是JAXB2.1,我对看到的XML输出感到困惑。下面我有两个子类,它们扩展了同一个父类。当在浏览器中使用REST进行编组并作为XML查看时,子类1(GeoLocationDecodedPayload)始终按预期具有GeoLocationDecodedPayload的根元素。由于某些原因,子类2(AltitudeDecodedPayload)没有将AltitudeDecodedPayload作为其根元素,这是在其@XMLRootElement注释中指定的意外情况。XML输出显示GeoPay
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() {}
}
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() {}
}
第二级儿童:
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() {}
}
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() {}
}
父类:
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() {}
}
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() {}
}
我忘了在下面的列表中包含AltitudeCodedPayLoad.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)
很抱歉,答案不是使用正确的父类和子类名称 这就是为什么我不明白解决办法