0

In the below code, I am expecting the attributes in the order of properties defined, but not able to get it. The order is always mixed up.

Parent Tag

@JacksonXmlRootElement
public class ParentSpec{

    @JacksonXmlProperty(localName = "parentSpec")
    @JacksonXmlElementWrapper
    public MySpec mySpec;
}

ChildTag

public class MySpec {

    @JacksonXmlProperty(localName = "mySpec")
    @JacksonXmlElementWrapper(useWrapping = false)
    public List<MyData> myDataList;

}

Error Child

@JsonPropertyOrder({"id", "type", "name", "data"})
public class MyData{

    @JacksonXmlProperty(isAttribute = true)
    public String id;
    @JacksonXmlProperty(isAttribute = true)
    public String type;
    @JacksonXmlProperty(isAttribute = true)
    public String name;
    @JacksonXmlProperty(isAttribute = true)
    public String data;
    
    //Other Pojo

}

I tried using the @JsonPropertyOrder, but it works only for elements it seems.

codeforHarman
  • 115
  • 1
  • 10
  • _"I am expecting the attributes in the order specified"_ Why are you expecting that? You haven't specified an order, so you're relying on the order the fields are returned by reflection, which - AFAIK - is not specified and may vary by Java version. – Mark Rotteveel Aug 23 '22 at 12:40
  • @MarkRotteveel Sorry for the confusion, i want to specify an order. Unable to do that using the JsonPropertyOrder – codeforHarman Aug 23 '22 at 12:45
  • So, show what you tried and how you are serializing the object. Create a [mre] to show what you do, show the actual and expected result. Because basically your question right now is "if I do nothing it has an undefined ordere", with a sidenote of "if I do something else it works for some, but not all". – Mark Rotteveel Aug 23 '22 at 12:47
  • @MarkRotteveel Made the edits, I want to custom order the properties. Also what is AFAIK? – codeforHarman Aug 23 '22 at 12:56
  • AFAIK - As Far As I Know. Your edit still is not a [mre] and does not provide actual and expected results. As an aside, in XML, attributes are unordered (see [In XML, is the attribute order important?](https://stackoverflow.com/questions/33746224/in-xml-is-the-attribute-order-important)), so I'm not actually sure you can achieve an ordering of attributes, nor should you have any technical reason to need such an order. – Mark Rotteveel Aug 23 '22 at 12:57
  • @MarkRotteveel I am facing an XML structure error, needed to verify it was due to the attribute order hence – codeforHarman Aug 23 '22 at 13:02

0 Answers0