简体   繁体   English

Jackson 上 XML 的序列化订单注释不起作用

[英]Serialising order annotations for XML on Jackson does not work

I already googled this problem for almost a day and tried several fixes (including 4 or 5 from StackOverflow) but none of them worked.我已经用谷歌搜索了将近一天的时间并尝试了几个修复程序(包括来自 StackOverflow 的 4 或 5 个),但没有一个有效。 I have this POJO with annotations to work with Jackson 2.10.2我有这个带有注释的 POJO 可以与 Jackson 2.10.2 一起使用

@JacksonXmlRootElement(localName = "request")
public class PdfExportQuery {

  @JacksonXmlProperty(localName = "user", isAttribute = true)
  private String username;

  @JacksonXmlProperty(localName = "pwd", isAttribute = true)
  private String password;

  @JacksonXmlProperty(localName = "putdoc")
  private XmlPutDoc putdoc;

  @JacksonXmlElementWrapper(useWrapping = false)
  @JacksonXmlProperty(localName = "import", isAttribute = true)
  private List<XmlPdfImport> imports;

  ... getters and setters
}

I use this POJO to create XML for some external requests.我使用这个 POJO 为一些外部请求创建 XML。 Everything works perfectly fine, but now I need to reorder the fields in the resulting XML and nothing seems to work.一切正常,但现在我需要重新排序结果 XML 中的字段,但似乎没有任何效果。

I tried first setting it up at class level:我首先尝试将其设置为 class 级别:

@JsonPropertyOrder({"putdoc"}) (Using only the field and localName)
or
@JsonPropertyOrder({"putdoc", "user", "pwd", "import"}) (Using all fields and localName)
or
@JsonPropertyOrder({"putdoc", "username", "password", "imports"}) (Using all fields and the java property name)

then I tried setting the order per field with:然后我尝试设置每个字段的顺序:

@JsonProperty(1)
or
@JsonProperty(value="putdoc", index = 1)

I tried also setting JsonProperty on all the fields and only on the field I am interested to appear at first.我还尝试在所有字段上设置 JsonProperty,并且只在我有兴趣首先出现的字段上设置。

I also tried:我也试过:

  • Removing all the @JacksonXmlProperty and leaving only @JsonProperty on each field删除所有 @JacksonXmlProperty 并在每个字段上仅保留 @JsonProperty
  • Combining @JsonPropertyOrder in the class and @JsonProperty on each field将 class 中的 @JsonPropertyOrder 和每个字段上的 @JsonProperty 结合起来
  • Checking that all annotations were imported from the com.fasterxml.jackson.annotation package检查是否所有注释都是从 com.fasterxml.jackson.annotation package 导入的

No matter what I do, the resulting XML always has the same ordering, all the ordering annotations seem to be ignored completely.无论我做什么,生成的 XML 始终具有相同的排序,所有排序注释似乎都被完全忽略了。

I cannot find a way to make it work with annotations.我找不到使它与注释一起使用的方法。 I needed to implement my custom serializer for the class just to change the order.我需要为 class 实现我的自定义序列化程序,只是为了更改顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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