简体   繁体   English

Springfox Swagger 如何配置命令属性?

[英]Configure how Springfox Swagger orders properties?

In my Spring Boot rest api, I have the following class:在我的 Spring Boot rest api 中,我有以下类:

@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.当我执行请求时,会遵守 JsonPropertyOrder,但是,在 Swagger UI(和 Swagger 文档)中,属性被列为描述、itemId、已查看。 Ie 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...它对我所有以常识/逻辑顺序排列的课程都这样做......

You can define the order in which the properties are going to be shown with ApiModelProperty#position .您可以使用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...这不是最方便的方法,但我找不到任何其他方法来实现这一目标......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM