Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating from Springfox Swagger 2 to Springdoc Open API

I try to follow these:

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

How do I deal with annotations like:

  • @ApiModel(value = "Response container")
  • @ApiModelProperty(value = "Iventory response", required = true)
like image 596
Kalpesh Soni Avatar asked Dec 11 '19 17:12

Kalpesh Soni


People also ask

How do I migrate from Swagger to OpenAPI?

Swagger Editor has a Convert to OpenAPI 3 option under the Edit drop-down that acts as an interface to this API. All we need to do is open our document in Swagger Editor, then convert by using Edit > Convert to OpenAPI 3. This will replace our document with the OAS 3.0 version. It worked!

Is Springdoc better than Springfox?

springdoc is a much more recent library that does not have so much legacy code as Springfox. As a consequence of the first one, springdoc is actually updated regularly and the amount of open issues is relatively small (only 11 vs 270 on Springfox).

Is OpenAPI the same as Swagger?

OpenAPI and Swagger used to refer to the same thing. While there are differences today (OpenAPI refers to RESTful API design and Swagger refers to a set of SmartBear tools), this blog will use the terms interchangeably. If you develop software today, chances are you are developing web APIs as well.

Is Springfox deprecated?

springfox (documentationProvider): Springfox is deprecated for removal in version 7.0. 0 of openapi-generator. The project seems to be no longer maintained (last commit is of Oct 14, 2020). It works with Spring Boot 2.5.


2 Answers

Migrating from SpringFox

  • Remove springfox and swagger 2 dependencies. Add springdoc-openapi-ui dependency instead.
   <dependency>       <groupId>org.springdoc</groupId>       <artifactId>springdoc-openapi-ui</artifactId>       <version>@springdoc.version@</version>    </dependency> 
  • Replace swagger 2 annotations with swagger 3 annotations (it is already included with springdoc-openapi-ui dependency). Package for swagger 3 annotations is io.swagger.v3.oas.annotations.

    • @ApiParam -> @Parameter
    • @ApiOperation -> @Operation
    • @Api -> @Tag
    • @ApiImplicitParams -> @Parameters
    • @ApiImplicitParam -> @Parameter
    • @ApiIgnore -> @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
    • @ApiModel -> @Schema
    • @ApiModelProperty -> @Schema
  • This step is optional: Only if you have multiple Docket beans replace them with GroupedOpenApi beans.

    Before:

        @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());     } 

    Now:

        @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();     } 

    If you have only one Docket -- remove it and instead add properties to your application.properties:

    springdoc.packagesToScan=package1, package2 springdoc.pathsToMatch=/v1, /api/balance/** 
  • Add bean of OpenAPI type. See example:

        @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"));     } 
  • If the swagger-ui is served behind a proxy:

    • https://springdoc.org/faq.html#how-can-i-deploy-the-doploy-springdoc-openapi-ui-behind-a-reverse-proxy.
  • To customise the Swagger UI

    • https://springdoc.org/faq.html#how-can-i-configure-swagger-ui.
  • To hide an operation or a controller from documentation

    • https://springdoc.org/faq.html#how-can-i-hide-an-operation-or-a-controller-from-documentation-.
like image 200
brianbro Avatar answered Sep 17 '22 07:09

brianbro


You can update Swagger2 annotations to Swagger3 (supported by springdoc).

Article with helpful regexps: https://www.david-merrick.com/2017/11/15/useful-regexes-for-transitioning-swagger-2-0-to-3-0-annotations/

Both @ApiModel and @ApiModelProperty need to be replaced with @Schema (io.swagger.v3.oas.annotations.media.Schema)

like image 28
Petr Aleksandrov Avatar answered Sep 21 '22 07:09

Petr Aleksandrov