java - springfox ApiModelProperty 位置排序不起作用

标签 java spring-boot swagger springfox

在我的 spring boot 应用程序中,我无法使用在 @ApiModel 注释类上正确排序的字段来管理我的 swagger JSON。

首先,我将 springfox 库导入到我的 pom.xml 中:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

其次,我创建了 SwaggerConfig.java:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)  
                  .select()                                  
                  .apis(RequestHandlerSelectors.basePackage("my.package.to.enable.swagger.doc"))           
                  .paths(PathSelectors.any())       
                  .build()
                  .host("http://localhost:8080");
    }
}

第三,我创建了由 @ApiModel 注释的 PersonDTO:

@ApiModel(value = "Person", description = "Person entity definition")
public class PersonDTO {

    @ApiModelProperty(value="Entity unique ID", position=0)
    public Long id;

    @ApiModelProperty(value="Person's number, useful to provide a convenient way to quickly communicate a person's reference", position=1)
    public Integer number;
    
    @ApiModelProperty(value="Person's first name", position=2)
    public String firstName;
    
    @ApiModelProperty(value="Person's last name", position=3)
    public String lastName;
    
}

然后,当我在 url http://localhost:[port]/[servlet-context-path]/v2/api-docs 请求 Json 输出时,位置顺序似乎不工作:

"definitions": {
    "Person": {
      "type": "object",
      "properties": {
        "firstName": {
          "type": "string",
          "description": "Person's first name"
        },
        "id": {
          "type": "integer",
          "format": "int64",
          "description": "Entity unique ID"
        },
        "lastName": {
          "type": "string",
          "description": "Person's last name"
        },
        "number": {
          "type": "integer",
          "format": "int32",
          "description": "Person's number, useful to provide a convenient way to quickly communicate a person's reference"
        }
      },
      "title": "Person",
      "description": "Person entity definition"
    }
}

最佳答案

目前这在 3.0.0 中已损坏,请参阅 https://github.com/springfox/springfox/issues/3391

关于java - springfox ApiModelProperty 位置排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63616811/

相关文章:

websocket - 通过Swagger描述websocket api

java - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name 'documentationPluginsBootstrapper' defined

database - 验证失败的: Detected applied migration not resolved locally | Flyway

java - org.modelmapper 中的标记化

java - 运行 junit 时不支持 Major.minor 版本 52.0

Java:在参数中传递指针中的对象(通过引用传递)

java - 如果 Kafka 和 Zookeeper 未运行,为什么 Maven Clean Install 无法完成?

spring-boot - 是否可以在与主应用程序不同的端口上运行 SpringFox 的 swagger 和 swagger-ui?

java - 端口 80 上的 Tomcat Web 应用程序

javascript - 如何自动调用 RESTful Web 服务