I’m using JAXB 2.1 and I’m confused by the XML output I’m seeing. 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. 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. The XML output shows the super class (GeoPayload) @XMLRootElement of geoPayload. Any ideas why these two class act differently?
child class 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:
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. This fixed my issue.