从springfox swagger2迁移到springdoc openapi

时间:2019-12-11 17:46:02

标签: swagger-2.0 openapi springdoc

https://www.dariawan.com/tutorials/spring/documenting-spring-boot-rest-api-springdoc-openapi-3/

尝试遵循这些

我如何处理类似的注释

@ApiModel(value = "Response container")

@ApiModelProperty(value = "Iventory response", required = true)

2 个答案:

答案 0 :(得分:21)

从SpringFox迁移

  • 删除springfox和swagger 2依赖项。添加springdoc-openapi-ui依赖项。
   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-ui</artifactId>
      <version>@springdoc.version@</version>
   </dependency>
  • 用不可靠的3个注释替换不可靠的2个注释(它已经包含在springdoc-openapi-ui依赖项中)。 摇摇欲坠的3个注释的软件包为io.swagger.v3.oas.annotations

    • @ApiParam-> @Parameter
    • @ApiOperation-> @Operation
    • @Api-> @Tag
    • @ApiImplicitParams-> @Parameters
    • @ApiImplicitParam-> @Parameter
    • @ApiIgnore-> @Parameter(hidden = true)@Operation(hidden = true)@Hidden
    • @ApiModel-> @Schema
    • @ApiModelProperty-> @Schema
  • 此步骤是可选的:仅当您有多个 Docket个bean时,才能将它们替换为GroupedOpenApi个bean。

    之前:

        @Bean
        public Docket publicApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("org.github.springshop.web.public"))
                    .paths(PathSelectors.regex("/public.*"))
                    .build()
                    .groupName("springshop-public")
                    .apiInfo(apiInfo());
        }
    
        @Bean
        public Docket adminApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("org.github.springshop.web.admin"))
                    .paths(PathSelectors.regex("/admin.*"))
                    .build()
                    .groupName("springshop-admin")
                    .apiInfo(apiInfo());
        }
    

    现在:

        @Bean
        public GroupedOpenApi publicApi() {
            return GroupedOpenApi.builder()
                    .setGroup("springshop-public")
                    .pathsToMatch("/public/**")
                    .build();
        }
    
        @Bean
        public GroupedOpenApi adminApi() {
            return GroupedOpenApi.builder()
                    .setGroup("springshop-admin")
                    .pathsToMatch("/admin/**")
                    .build();
        }
    

    如果只有 Docket,请删除它,然后将属性添加到您的application.properties

    springdoc.packagesToScan=package1, package2
    springdoc.pathsToMatch=/v1, /api/balance/**
    
  • 添加OpenAPI类型的bean。参见示例:

        @Bean
        public OpenAPI springShopOpenAPI() {
            return new OpenAPI()
                    .info(new Info().title("SpringShop API")
                    .description("Spring shop sample application")
                    .version("v0.0.1")
                    .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                    .externalDocs(new ExternalDocumentation()
                    .description("SpringShop Wiki Documentation")
                    .url("https://springshop.wiki.github.org/docs"));
        }
    
  • 如果swagger-ui是在代理后面提供的:

  • 自定义Swagger UI

  • 从文档中隐藏操作或控制器

答案 1 :(得分:4)

您可以将Swagger2注释更新为Swagger3(受springdoc支持)。

带有有用的正则表达式的文章: https://www.david-merrick.com/2017/11/15/useful-regexes-for-transitioning-swagger-2-0-to-3-0-annotations/

@ApiModel@ApiModelProperty都需要替换为@Schemaio.swagger.v3.oas.annotations.media.Schema