'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. I have this POJO with annotations to work with 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. Everything works perfectly fine, but now I need to reorder the fields in the resulting XML and nothing seems to work.

I tried first setting it up at class level:

@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.

I also tried:

  • Removing all the @JacksonXmlProperty and leaving only @JsonProperty on each field
  • Combining @JsonPropertyOrder in the class and @JsonProperty on each field
  • Checking that all annotations were imported from the 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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 holyknight