Lets say i have class
@XmlType(propOrder = {
"one",
"two"
})
@XmlRootElement(name = "search")
public class Search {
protected One one;
protected Two two;
//getters & setters
}
and i wanted a class that extends this class
e.g.
@XmlType(propOrder = {
"three"
})
@XmlRootElement(name = "searchExtended")
public class SearchExtended extends Search {
protected Three three;
//getters & setters
}
how do you declare the propOrder correctly, i have tried this before and it did not use the order i thought it would. How is this handled by annotations? / how should you be declaring this across extended classes?
The parents properties will be marshalled based on their specified ordering before the child properties. You can include the properties from the parent class in the propOrder
of the child class if you annotate the parent class with @XmlTransient
.
UPDATE
Is there a way i can make it transistant but still use it normally?
No, setting @XmlTransient
on a class removes it from the classes that JAXB considers mapped. The reason that JAXB marhals the properties of the super class before the properties of the subclass is to match the rules of XML schema. When your Search
class is not marked with @XmlTransient
the corresponding XML schema is the following. According to the XML schema rules in order for a element of type searchExtended
to be valid the elements from the super type must occur before any elements defined in the subtype.
<xs:complexType name="searchExtended">
<xs:complexContent>
<xs:extension base="search">
<xs:sequence>
<xs:element name="three" type="three" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="search">
<xs:sequence>
<xs:element name="one" type="one" minOccurs="0"/>
<xs:element name="two" type="two" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
You can see the XML schema that corresponds to your JAXB model by running the following code:
import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(SearchExtended.class);
jc.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String namespaceUri,
String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(suggestedFileName);
return result;
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With