0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
holyknight
  • 315
  • 1
  • 3
  • 13
  • Does this answer your question? [Jackson ObjectMapper - specify serialization order of object properties](https://stackoverflow.com/questions/27577701/jackson-objectmapper-specify-serialization-order-of-object-properties) – Lakshan May 05 '22 at 14:27
  • No, i already tried that. I needed to implement a custom serializer just to get the order right. – holyknight May 06 '22 at 12:02

1 Answers1

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.

holyknight
  • 315
  • 1
  • 3
  • 13