JsonPropertyOrder未能正确排序

huangapple go评论104阅读模式
英文:

JsonPropertyOrder not ordering correctly

问题

  1. @JacksonXmlRootElement(localName = "xmlroot")
  2. @JsonPropertyOrder({"cnt-unik-id","kod-produktu","frekvence","datum-sjednani",
  3. "pocatek","konec","spravce","ziskatele","objekty-unik-id","udaje","objekty-all","adresy","zpp"})
  4. public class ContractDetail{
  5. @JacksonXmlProperty(localName = "zpp")
  6. private Integer zpplID;
  7. @JacksonXmlProperty(localName = "cnt-unik-id")
  8. private Integer id;
  9. @JacksonXmlProperty(localName = "kod-produktu")
  10. private Item product;
  11. @JacksonXmlProperty(localName = "spravce")
  12. private Item administrator;
  13. @JacksonXmlElementWrapper(localName = "ziskatele")
  14. @JacksonXmlProperty(localName = "xml-ziskatel")
  15. private List<Customer> customers;
  16. @JacksonXmlProperty(localName = "frekvence")
  17. private Item frequency;
  18. @JacksonXmlProperty(localName = "datum-sjednani")
  19. private Item createdAt;
  20. @JacksonXmlProperty(localName = "pocatek")
  21. private Item startDate;
  22. @JacksonXmlProperty(localName = "konec")
  23. private Item endDate;
  24. @JacksonXmlElementWrapper(localName = "objekty-unik-id")
  25. @JacksonXmlProperty(localName = "int")
  26. private List<Integer> vehicle;
  27. @JacksonXmlProperty(localName = "xml-hodnota")
  28. @JacksonXmlElementWrapper(localName = "udaje")
  29. private List<Item> values;
  30. @JacksonXmlProperty(localName = "xml-objekt")
  31. @JacksonXmlElementWrapper(localName = "objekty-all")
  32. private List<ObjectItem> objects;
  33. @JacksonXmlElementWrapper(localName = "adresy")
  34. @JacksonXmlProperty(localName = "xml-adresa")
  35. private List<AddressItem> address;
  36. //getters setters constructors stuff
  37. }
英文:

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 JsonPropertyOrder未能正确排序

(JacksonXML ver 2.9.8)

  1. @JacksonXmlRootElement(localName = &quot;xmlroot&quot;)
  2. @JsonPropertyOrder({&quot;cnt-unik-id&quot;,&quot;kod-produktu&quot;,&quot;frekvence&quot;,&quot;datum-sjednani&quot;,
  3. &quot;pocatek&quot;,&quot;konec&quot;,&quot;spravce&quot;,&quot;ziskatele&quot;,&quot;objekty-unik-id&quot;,&quot;udaje&quot;,&quot;objekty-all&quot;,&quot;adresy&quot;,&quot;zpp&quot;})
  4. public class ContractDetail{
  5. @JacksonXmlProperty(localName = &quot;zpp&quot;)
  6. private Integer zpplID;
  7. @JacksonXmlProperty(localName = &quot;cnt-unik-id&quot;)
  8. private Integer id;
  9. @JacksonXmlProperty(localName = &quot;kod-produktu&quot;)
  10. private Item product;
  11. @JacksonXmlProperty(localName = &quot;spravce&quot;)
  12. private Item administrator;
  13. @JacksonXmlElementWrapper(localName = &quot;ziskatele&quot;)
  14. @JacksonXmlProperty(localName = &quot;xml-ziskatel&quot;)
  15. private List&lt;Customer&gt; customers;
  16. @JacksonXmlProperty(localName = &quot;frekvence&quot;)
  17. private Item frequency;
  18. @JacksonXmlProperty(localName = &quot;datum-sjednani&quot;)
  19. private Item createdAt;
  20. @JacksonXmlProperty(localName = &quot;pocatek&quot;)
  21. private Item startDate;
  22. @JacksonXmlProperty(localName = &quot;konec&quot;)
  23. private Item endDate;
  24. @JacksonXmlElementWrapper(localName = &quot;objekty-unik-id&quot;)
  25. @JacksonXmlProperty(localName = &quot;int&quot;)
  26. private List&lt;Integer&gt; vehicle;
  27. @JacksonXmlProperty(localName = &quot;xml-hodnota&quot;)
  28. @JacksonXmlElementWrapper(localName = &quot;udaje&quot;)
  29. private List&lt;Item&gt; values;
  30. @JacksonXmlProperty(localName = &quot;xml-objekt&quot;)
  31. @JacksonXmlElementWrapper(localName = &quot;objekty-all&quot;)
  32. private List&lt;ObjectItem&gt; objects;
  33. @JacksonXmlElementWrapper(localName = &quot;adresy&quot;)
  34. @JacksonXmlProperty(localName = &quot;xml-adresa&quot;)
  35. private List&lt;AddressItem&gt; address;
  36. //getters setters contructors stuff
  37. }

答案1

得分: 1

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

例如,使用您的ContractDetail类的简化版本:

使用此代码:

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

生成以下XML:

  1. <xmlroot>
  2. <cnt-unik-id>123</cnt-unik-id>
  3. <objekty-unik-id>
  4. <int>678</int>
  5. <int>789</int>
  6. </objekty-unik-id>
  7. <zpplID>456</zpplID>
  8. </xmlroot>

以及使用此代码:

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

生成以下XML:

  1. <xmlroot>
  2. <objekty-unik-id>
  3. <int>678</int>
  4. <int>789</int>
  5. </objekty-unik-id>
  6. <zpplID>456</zpplID>
  7. <cnt-unik-id>123</cnt-unik-id>
  8. </xmlroot>
英文:

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

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

Using this:

  1. @JsonPropertyOrder({&quot;id&quot;, &quot;vehicle&quot;, &quot;zpplID&quot;})

Generates this:

  1. &lt;xmlroot&gt;
  2. &lt;cnt-unik-id&gt;123&lt;/cnt-unik-id&gt;
  3. &lt;objekty-unik-id&gt;
  4. &lt;int&gt;678&lt;/int&gt;
  5. &lt;int&gt;789&lt;/int&gt;
  6. &lt;/objekty-unik-id&gt;
  7. &lt;zpplID&gt;456&lt;/zpplID&gt;
  8. &lt;/xmlroot&gt;

And using this:

  1. @JsonPropertyOrder({&quot;vehicle&quot;, &quot;zpplID&quot;, &quot;id&quot;})

Generates this:

  1. &lt;xmlroot&gt;
  2. &lt;objekty-unik-id&gt;
  3. &lt;int&gt;678&lt;/int&gt;
  4. &lt;int&gt;789&lt;/int&gt;
  5. &lt;/objekty-unik-id&gt;
  6. &lt;zpplID&gt;456&lt;/zpplID&gt;
  7. &lt;cnt-unik-id&gt;123&lt;/cnt-unik-id&gt;
  8. &lt;/xmlroot&gt;

答案2

得分: 1

  1. 所以 @andrewjames 的答案是有效的,但如果仍然有人需要/想要使用 XML 元素名称,我提出的解决方案如下:
  2. @JsonPropertyOrder({"cnt-unik-id","kod-produktu","frekvence","datum-sjednani",
  3. "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:

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

huangapple
  • 本文由 发表于 2020年9月11日 06:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63838259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定