Swagger2 WebFlux

  java, swagger

Order

This article mainly shows how to use Swagger that supports WebFlux.

maven

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-webflux</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
  • Swagger.version is currently 3.0.0-SNAPSHOT, so it has not been released into maven’s official warehouse and needs to be pulled from jcenter-snapshots.
    <repositories>
        <repository>
            <id>jcenter-snapshots</id>
            <name>jcenter</name>
            <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
        </repository>
    </repositories>

Configuration

@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .description("example api")
                        .title("example api")
                        .version("1.0.0")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build();

    }
}
  • Due to the support of WebFlux, the previous @EnableSwagger2 was removed and changed to @EnableSwagger2WebMvc and @EnableSwagger2WebFlux, which is used here.

Summary

  • Spring 5 introduced WebFlux, while the current version of SpringFox Swagger2 (2.9.2) WebFlux is not yet supported and only 3.0.0-SNAPSHOT is required.
  • Since it is a SNAPSHOT version, it has not been released into maven’s official warehouse and needs to be pulled from jcenter-snapshots. in addition, to use Swagger2 that supports WebFlux, springfox-spring-webflux dependency needs to be introduced.
  • Since WebFlux is supported, the previous @EnableSwagger2 has been removed and changed to @EnableSwagger2WebMvc and @EnableSwagger2WebFlux.

doc