JsonPropertyOrder未能正确排序

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

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

(JacksonXML ver 2.9.8)

@JacksonXmlRootElement(localName = &quot;xmlroot&quot;)
@JsonPropertyOrder({&quot;cnt-unik-id&quot;,&quot;kod-produktu&quot;,&quot;frekvence&quot;,&quot;datum-sjednani&quot;,
&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;})
public class ContractDetail{
@JacksonXmlProperty(localName = &quot;zpp&quot;)
private Integer zpplID;
@JacksonXmlProperty(localName = &quot;cnt-unik-id&quot;)
private Integer id;
@JacksonXmlProperty(localName = &quot;kod-produktu&quot;)
private Item product;
@JacksonXmlProperty(localName = &quot;spravce&quot;)
private Item administrator;
@JacksonXmlElementWrapper(localName = &quot;ziskatele&quot;)
@JacksonXmlProperty(localName = &quot;xml-ziskatel&quot;)
private List&lt;Customer&gt; customers;
@JacksonXmlProperty(localName = &quot;frekvence&quot;)
private Item frequency;
@JacksonXmlProperty(localName = &quot;datum-sjednani&quot;)
private Item createdAt;
@JacksonXmlProperty(localName = &quot;pocatek&quot;)
private Item startDate;
@JacksonXmlProperty(localName = &quot;konec&quot;)
private Item endDate;
@JacksonXmlElementWrapper(localName = &quot;objekty-unik-id&quot;)
@JacksonXmlProperty(localName = &quot;int&quot;)
private List&lt;Integer&gt; vehicle;
@JacksonXmlProperty(localName = &quot;xml-hodnota&quot;)
@JacksonXmlElementWrapper(localName = &quot;udaje&quot;)
private List&lt;Item&gt; values;
@JacksonXmlProperty(localName = &quot;xml-objekt&quot;)
@JacksonXmlElementWrapper(localName = &quot;objekty-all&quot;)
private List&lt;ObjectItem&gt; objects;
@JacksonXmlElementWrapper(localName = &quot;adresy&quot;)
@JacksonXmlProperty(localName = &quot;xml-adresa&quot;)
private List&lt;AddressItem&gt; 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({&quot;id&quot;, &quot;vehicle&quot;, &quot;zpplID&quot;})

Generates this:

&lt;xmlroot&gt;
&lt;cnt-unik-id&gt;123&lt;/cnt-unik-id&gt;
&lt;objekty-unik-id&gt;
&lt;int&gt;678&lt;/int&gt;
&lt;int&gt;789&lt;/int&gt;
&lt;/objekty-unik-id&gt;
&lt;zpplID&gt;456&lt;/zpplID&gt;
&lt;/xmlroot&gt;

And using this:

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

Generates this:

&lt;xmlroot&gt;
&lt;objekty-unik-id&gt;
&lt;int&gt;678&lt;/int&gt;
&lt;int&gt;789&lt;/int&gt;
&lt;/objekty-unik-id&gt;
&lt;zpplID&gt;456&lt;/zpplID&gt;
&lt;cnt-unik-id&gt;123&lt;/cnt-unik-id&gt;
&lt;/xmlroot&gt;

答案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({&quot;cnt-unik-id&quot;,&quot;kod-produktu&quot;,&quot;frekvence&quot;,&quot;datum-sjednani&quot;,
&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:

确定