15

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?

S. Pauk
  • 5,208
  • 4
  • 31
  • 41

3 Answers3

16

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;

}
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • 5
    Thx, I know of this option. Question was about triggering it on a higher level with just one setting (imagine a payload with 10-20-30.. fields). – S. Pauk Feb 23 '18 at 14:16
  • In that case it probably would have to use reflection, and from the following links, using reflection to retrieve fields doesn't respect their appearance order (https://groups.google.com/forum/#!topic/swagger-swaggersocket/uIRLoKVJPFw and https://stackoverflow.com/questions/1097807/java-reflection-is-the-order-of-class-fields-and-methods-standardized). – Arnaud Feb 23 '18 at 14:29
  • 4
    OK, then the short answer seems to be "no" – S. Pauk Feb 23 '18 at 15:10
  • 3
    Is there any workaround at global level? or Can configuration level ? – PAA Nov 26 '19 at 09:48
  • 1
    This didn't work for me in SpringFox v3.0.0 – Doctor Parameter Jun 18 '21 at 17:36
0

Looks like there is no good generic solution yet. I’d suggest to follow/upvote this issue on github: https://github.com/springfox/springfox/issues/3853

The project is not well maintained though, last commits are from 1 year ago.

Yaroslav
  • 81
  • 1
  • 3
-3

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

Jaison Varghese
  • 1,302
  • 1
  • 13
  • 24