Search code examples
javaspringxml-serializationjackson-dataformat-xml

JsonPropertyOrder not ordering correctly


so as you can see from the title my @JsonPropertyOrder is not ordering how I want... this is my class(see code bellow) and everything is ordered good except the zpp attribute, it goes between "spravce" and "ziskatele". I tried to rename it reorder it and its totally ignored.Thank you for all the answers :)

(JacksonXML ver 2.9.8)

@JacksonXmlRootElement(localName = "xmlroot")
@JsonPropertyOrder({"cnt-unik-id","kod-produktu","frekvence","datum-sjednani",
"pocatek","konec","spravce","ziskatele","objekty-unik-id","udaje","objekty-all","adresy","zpp"})
public class ContractDetail{

    @JacksonXmlProperty(localName = "zpp")
    private Integer zpplID;

    @JacksonXmlProperty(localName = "cnt-unik-id")
    private Integer id;

    @JacksonXmlProperty(localName = "kod-produktu")
    private Item product;

    @JacksonXmlProperty(localName = "spravce")
    private Item administrator;

    @JacksonXmlElementWrapper(localName = "ziskatele")
    @JacksonXmlProperty(localName = "xml-ziskatel")
    private List<Customer> customers;

    @JacksonXmlProperty(localName = "frekvence")
    private Item frequency;

    @JacksonXmlProperty(localName = "datum-sjednani")
    private Item createdAt;

    @JacksonXmlProperty(localName = "pocatek")
    private Item startDate;

    @JacksonXmlProperty(localName = "konec")
    private Item endDate;

    @JacksonXmlElementWrapper(localName = "objekty-unik-id")
    @JacksonXmlProperty(localName = "int")
    private List<Integer> vehicle;

    @JacksonXmlProperty(localName = "xml-hodnota")
    @JacksonXmlElementWrapper(localName = "udaje")
    private List<Item> values;

    @JacksonXmlProperty(localName = "xml-objekt")
    @JacksonXmlElementWrapper(localName = "objekty-all")
    private List<ObjectItem> objects;

    @JacksonXmlElementWrapper(localName = "adresy")
    @JacksonXmlProperty(localName = "xml-adresa")
    private List<AddressItem> address;

    //getters setters contructors stuff

}

Solution

  • Use the Java field names, instead of the XML element names.

    For example, using a simplified version of your ContractDetail class:

    Using this:

    @JsonPropertyOrder({"id", "vehicle", "zpplID"})
    

    Generates this:

    <xmlroot>
        <cnt-unik-id>123</cnt-unik-id>
        <objekty-unik-id>
            <int>678</int>
            <int>789</int>
        </objekty-unik-id>
        <zpplID>456</zpplID>
    </xmlroot>
    

    And using this:

    @JsonPropertyOrder({"vehicle", "zpplID", "id"})
    

    Generates this:

    <xmlroot>
        <objekty-unik-id>
            <int>678</int>
            <int>789</int>
        </objekty-unik-id>
        <zpplID>456</zpplID>
        <cnt-unik-id>123</cnt-unik-id>
    </xmlroot>