英文:
JsonPropertyOrder not ordering correctly
问题
@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 constructors stuff
}
英文:
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
}
答案1
得分: 1
使用Java字段名称,而不是XML元素名称。
例如,使用您的ContractDetail
类的简化版本:
使用此代码:
@JsonPropertyOrder({"id", "vehicle", "zpplID"})
生成以下XML:
<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"})
生成以下XML:
<xmlroot>
<objekty-unik-id>
<int>678</int>
<int>789</int>
</objekty-unik-id>
<zpplID>456</zpplID>
<cnt-unik-id>123</cnt-unik-id>
</xmlroot>
英文:
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>
答案2
得分: 1
所以 @andrewjames 的答案是有效的,但如果仍然有人需要/想要使用 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"})
英文:
Soo @andrewjames's answer works but If someone still needs/wants to use xml element names the solution I came up with looks like this:
@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"})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论