在Spring Webflux中使用Swagger生成Web服务描述



有人在Spring webflux环境中使用Swagger库描述Web服务的解决方案吗?

目标是使用 Swagger 自动生成 ws 客户端存根。

变通方法,直到Springfox 3.0.0不可用

聚球文件

       <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-webflux</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
 <repositories>
        <repository>
            <id>spring-libs-milestone</id>
            <name>Spring Milestone Maven Repository</name>
            <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
        </repository>
    </repositories>

配置

@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig  implements WebFluxConfigurer {
    @Bean
    public Docket api() {
       
        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(Mono.class, Flux.class, Publisher.class)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger**")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

更新

springfox-boot-starter现在可用,它与 webflux 一起工作。我们只需要在pom文件中添加下面的启动项目。注意:在类路径中springfox-boot-starter我们不需要@EnableSwagger2WebFlux

删除对springfox-swagger2的显式依赖

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${io.springfox}</version>
        </dependency>  
Spring

boot 2.x 和 spring 5 功能从 2.8.0 开始不受 Spring Fox 支持。

您可能希望订阅以下 Springfox 问题:https://github.com/springfox/springfox/issues/1773

在等待 springfox 项目的官方 webflux 支持时,在 GitHub 上通过 deblockt 完成了一个解决方法:https://github.com/deblockt/springfox/tree/feature/webflux。

只需查看 sourecode 并构建您自己的 jar,将它们包含在您的项目中。

从版本 2.9.2 开始,您可以将 swagger 与 webflux 一起使用。

https://github.com/deblockt/springfox/tree/feature/webflux

我在github中配置了Spring boot 2 Servlet堆栈,项目。聊胜于无。:)https://github.com/armdev/springboot2-swagger

您可以使用 spring-boot-starter-web 以及 spring-boot-starter-webflux。因此,如果你真的想在Spring webflux中使用swagger,那么你必须在项目中添加spring-boot-starter-web depdendence。你现在可以使用大摇大摆,就像你用过弹簧网一样。不会有任何区别。

最新更新