JsonPropertyOrder 排序不正确

JsonPropertyOrder not ordering correctly

因此,正如您从标题中看到的那样,我的@JsonPropertyOrder 没有按我想要的方式排序...这是我的 class(请参阅下面的代码),除了 zpp 属性外,一切都很好,它会在“spravce”和“ziskatele”之间。我试着重命名它重新排序它完全 ignored.Thank 你所有的答案:)

(JacksonXML 版本 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

}

使用 Java 字段名称,而不是 XML 元素名称。

例如,使用您的 ContractDetail class:

的简化版本

使用这个:

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

生成这个:

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

并使用这个:

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

生成这个:

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

Soo @andrewjames 的答案有效,但如果有人仍然 needs/wants 使用 xml 元素命名,我想出的解决方案如下所示:

@JsonPropertyOrder({"cnt-unik-id","kod-produktu","frekvence","datum-sjednani",
"pocatek","konec","spravce","ziskatele","objekty-unik-id","int","udaje","xml-hodnota","objekty-all","xml-objekt","adresy","xml-adresa","zpp"})