转载

Swagger2 WebFlux小试牛刀

本文主要展示一下如何使用支持WebFlux的Swagger

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目前是3.0.0-SNAPSHOT,因而没有发布到maven官方仓库里头,需要从jcenter-snapshots中拉取
<repositories>
        <repository>
            <id>jcenter-snapshots</id>
            <name>jcenter</name>
            <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
        </repository>
    </repositories>

配置

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

    }
}
  • 由于支持了WebFlux,所以之前的@EnableSwagger2就移除掉了,变为@EnableSwagger2WebMvc以及@EnableSwagger2WebFlux,这里使用的是@EnableSwagger2WebFlux

小结

2.9.2

doc

  • springfox docs snapshot
  • Maven repository for springfox-spring-webflux ? #2939
  • spring-boot-webflux-swagger-starter
原文  https://segmentfault.com/a/1190000018826841
正文到此结束
Loading...