Estoy usando ObjectMapper para hacer mi mapeo java-json.
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
ow.writeValue(new File( fileName +".json"), jsonObj);
esta es mi clase de java:
public class Relation {
private String id;
private String source;
private String target;
private String label;
private List<RelAttribute> attributes;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getSource() {
    return source;
}
public void setSource(String source) {
    this.source = source;
}
public String getTarget() {
    return target;
}
public void setTarget(String target) {
    this.target = target;
}
public String getLabel() {
    return label;
}
public void setLabel(String label) {
    this.label = label;
}
public void setAttributes(List<RelAttribute> attributes) {
    this.attributes = attributes;
}
public List<RelAttribute> getAttributes() {
    return attributes;
}
}
esto es lo que obtengo:
{
    "id" : "-75da69d3-79c8-4000-a3d8-b10350a57a7e",
    "attributes" : [ {
      "attrName" : "ID",
      "attrValue" : ""
    }, {
      "attrName" : "Description",
      "attrValue" : "Primary Actor"
    }, {
      "attrName" : "Status",
      "attrValue" : ""
    } ],
    "label" : "new Label",
    "target" : "-46b238ac-b8b3-4230-b32c-be9707f8b691",
    "source" : "-daa34638-061a-45e0-9f2e-35afd6c271e0"
  }
Entonces mi pregunta ahora es, ¿cómo puedo obtener esta salida json?
{
    "id" : "-75da69d3-79c8-4000-a3d8-b10350a57a7e",
    "label" : "new Label",
    "target" : "-46b238ac-b8b3-4230-b32c-be9707f8b691",
    "source" : "-daa34638-061a-45e0-9f2e-35afd6c271e0",
    "attributes" : [ {
      "attrName" : "ID",
      "attrValue" : ""
    }, {
      "attrName" : "Description",
      "attrValue" : "Primary Actor"
    }, {
      "attrName" : "Status",
      "attrValue" : ""
    } ]
  }
Lo quiero con el mismo orden que en mi declaración de Java. ¿Hay alguna forma de especificarlo? ¿Quizás con anotaciones o cosas así?

Respuestas:
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" }) public class Relation { ... }fuente
¿Sabe que existe una forma conveniente de especificar el orden alfabético?
@JsonPropertyOrder(alphabetic = true) public class Relation { ... }Si tiene requisitos específicos, a continuación, le mostramos cómo configurar los pedidos personalizados:
@JsonPropertyOrder({ "id", "label", "target", "source", "attributes" }) public class Relation { ... }fuente
Puedes usar
@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "response", propOrder = { "prop1", "prop2", "prop3", "prop4", "prop5", "prop6" }).@JsonPropertyOrderrequiere que se agregue un nuevo frasco.fuente