简体   繁体   中英

Jackson XmlMapper generates wrong XML entries order

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({ "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;

    // all getters and setters ...

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>
  • 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 :-)

Thank you and best regards

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM