Jackson XmlMapper 生成的 XML 条目顺序错误

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

Jackson XmlMapper generates wrong XML entries order

问题

使用Java Jackson的XmlMapper生成XML字符串时我遇到了一个问题它生成的XML字符串中条目的顺序是错误的尽管我使用了@JsonPropertyOrder并且类内部的成员也是需要的顺序
请参考我的代码

@JsonPropertyOrder({ "craneNumber", "moveType", "reference", "unitNumber", "ISOCode", "IMOLabels", "seal", "doorDirection" })
public class OcrDataResultUnit {
    @JacksonXmlElementWrapper(localName="unit")
    private String craneNumber;
    private String moveType;
    private String reference;
    private String unitNumber;
    @JsonProperty("ISOCode")
    private String isoCode;
    @JacksonXmlElementWrapper(localName="IMOLabels")
    @JsonProperty("DGSIMOClass")
    private List<String> imoLabels = new ArrayList<>();
    @JsonProperty("seal")
    private String seal;
    @JsonProperty("doorDirection")
    private String doorDirection;

    // 所有的getter和setter...

Usage:

    XmlMapper mapper = new XmlMapper();
    String msgXml = mapper.writeValueAsString(this);

Result:

<unit>
<craneNumber>QC01</craneNumber>
<moveType>D</moveType>
<reference>12345678901234567890123456789012</reference>
<unitNumber>ABCD00001234</unitNumber>
<ISOCode>22G1</ISOCode>
<seal>Y</seal>
<doorDirection>H</doorDirection>
<IMOLabels>
<DGSIMOClass>1.5</DGSIMOClass>
<DGSIMOClass>2.1</DGSIMOClass>
</IMOLabels>
</unit>

* 在最后两个成员上不加 @JsonProperty 我得到了相同的结果这是一个尝试
* 这个结构是更大的XML结构的一部分
* 同样将 @JsonProperty 替换为 @JacksonXmlProperty结果相同
* 据我所见@JsonPropertyOrder 在XML中也是正确使用的

是否有人有想法
也许我只是疏忽了 - 实际上我希望是这样的 :-)

谢谢并致以最诚挚的问候
英文:

I have an issue with generating a XML string with the Java Jackson XmlMapper: It generates the wrong order of entries in the XML string, nevertheless I use @JsonPropertyOrder and the members are in the needed order inside the class.
Please see my code:

@JsonPropertyOrder({ &quot;craneNumber&quot;, &quot;moveType&quot;, &quot;reference&quot;, &quot;unitNumber&quot;, &quot;ISOCode&quot;, &quot;IMOLabels&quot;, &quot;seal&quot;, &quot;doorDirection&quot; })
public class OcrDataResultUnit {
@JacksonXmlElementWrapper(localName=&quot;unit&quot;)
private String craneNumber;
private String moveType;
private String reference;
private String unitNumber;
@JsonProperty(&quot;ISOCode&quot;)
private String isoCode;
@JacksonXmlElementWrapper(localName=&quot;IMOLabels&quot;)
@JsonProperty(&quot;DGSIMOClass&quot;)
private List&lt;String&gt; imoLabels = new ArrayList&lt;&gt;();
@JsonProperty(&quot;seal&quot;)
private String seal;
@JsonProperty(&quot;doorDirection&quot;)
private String doorDirection;
// all getters and setters ...

Usage:

    XmlMapper mapper = new XmlMapper();
String msgXml = mapper.writeValueAsString(this);

Result:

&lt;unit&gt;
&lt;craneNumber&gt;QC01&lt;/craneNumber&gt;
&lt;moveType&gt;D&lt;/moveType&gt;
&lt;reference&gt;12345678901234567890123456789012&lt;/reference&gt;
&lt;unitNumber&gt;ABCD00001234&lt;/unitNumber&gt;
&lt;ISOCode&gt;22G1&lt;/ISOCode&gt;
&lt;seal&gt;Y&lt;/seal&gt;
&lt;doorDirection&gt;H&lt;/doorDirection&gt;
&lt;IMOLabels&gt;
&lt;DGSIMOClass&gt;1.5&lt;/DGSIMOClass&gt;
&lt;DGSIMOClass&gt;2.1&lt;/DGSIMOClass&gt;
&lt;/IMOLabels&gt;
&lt;/unit&gt;
  • I get the same result without the @JsonProperty on the last to members. That was a try.
  • The structure is part of a bigger XML structure.
  • Also replaced @JsonProperty with @JacksonXmlProperty: Same result.
  • As far as I see @JsonPropertyOrder is correct to be used for XML as well.

Does anybody have an idea?
Maybe I am just blind - actually I hope so Jackson XmlMapper 生成的 XML 条目顺序错误

Thank you and best regards

答案1

得分: 0

在您的JsonPropertyOrder注解中,属性被称为"DGSIMOClass",而不是"IMOLabels"。您应该将其替换为正确的名称。

英文:

In your JsonPropertyOrder annotation, the property is called "DGSIMOClass", not "IMOLabels". You should switch it out for the correct name.

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

发表评论

匿名网友

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

确定