Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure how Springfox Swagger orders properties?

In my Spring Boot rest api, I have the following class:

@Entity
@Table(name="Items")
@JsonPropertyOrder({ "itemId", "description", "viewed" })
public class Item {

    @ApiModelProperty(notes="Id of the item.", required=true, value="100000")
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @JsonProperty(access=Access.READ_ONLY)
    private int itemId = 0;
    @ApiModelProperty(notes="Item description.", required=true, value="Item1")
    @NotNull
    @Size(min=1, max=256)
    private String description;
    private int viewed;

    public int getItemId() {
        return this.itemId;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getViewed() {
        return this.viewed;
    }
}

When I execute the request, the JsonPropertyOrder is respected, however, in the Swagger UI (and the Swagger doc), the properties are listed as description, itemId, viewed. I.e. alphabetical. I never turned on alphabetical sorting, so not sure why its doing that... any way to turn that off? It's doing that to all my classes which are laid out in common sense / logical order...

like image 682
SledgeHammer Avatar asked Oct 11 '25 12:10

SledgeHammer


1 Answers

You can define the order in which the properties are going to be shown with ApiModelProperty#position.

Example:

class MyClass {
  @ApiModelProperty(position = 0)
  String myFirstProperty;

  @ApiModelProperty(position = 1)
  String mySecondProperty;
}

It's not the most convenient method, but I couldn't find any other way to achieve this...

like image 97
Gustavo Passini Avatar answered Oct 14 '25 03:10

Gustavo Passini