英文:
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({ "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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论