Swagger+Spring: is it possible to preserve the fie

2020-08-22 04:19发布

问题:

Assuming my payload class is:

public class Payload {
  private final long id;
  private final String aField;
}

springfox will sort the payload fields in the lexicographical order which will produce the following payload spec:

{
  "aField": "string",
  "id": 0
} 

Is there any control parameter which tells the springfox to preserve the original fields order?

回答1:

You may use @ApiModelProperty and specify a position :

public class Payload {

  @ApiModelProperty(value = "The id", position = 1)
  private final long id;
  @ApiModelProperty(value = "The a field", position = 2)
  private final String aField;

}


回答2:

You can use the @JsonPropertyOrder once at the top of your class and the fields will be ordered in the order they are defined in the class https://www.logicbig.com/tutorials/misc/jackson/json-property-order-annotation.html