webflux整合swagger教程

相信已经有不少人上手了 webflux,包括我之前也写了很多类似的整合教程,但是在整合 swagger 框架方面,我还是第一次尝试。

webflux 和 springmvc 不同,webflux 返回的 Mono、Flux 不能被 swagger
框架正常识别。因此我们要引入 springfox-spring-webflux,否则的话需要写很多的适配代码,才能让 swagger 兼容
Mono、Flux。

下面我们一起开始本次教程的整合之旅,我这里的 swagger 版本是 3.0.0,2.10.x 版本的也可以整合,具体看你个人兴趣和你使用的
spring-boot-starter-webflux 版本。

1.首先是 pom.xml 中的代码。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    
    <!--数据库开始-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>
    <!--数据库结束-->
	<!--整合swagger开始-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-spring-webflux</artifactId>
        <version>3.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.plugin</groupId>
                <artifactId>spring-plugin-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.plugin</groupId>
                <artifactId>spring-plugin-metadata</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.plugin</groupId>
                <artifactId>spring-plugin-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.plugin</groupId>
                <artifactId>spring-plugin-metadata</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>
	<!--整合swagger结束-->
	<!--引入更高版本开始-->
    <dependency>
        <groupId>org.springframework.plugin</groupId>
        <artifactId>spring-plugin-core</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.plugin</groupId>
        <artifactId>spring-plugin-metadata</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
    <!--引入更高版本结束-->
</dependencies>

pom 中引入 springfox-spring-webflux 后,我们需要把 spring-plugin-core
排除掉,否则会启动报错,抛出下面类似的异常。

ERROR 2368 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
 
***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
An attempt was made to call a method that does not exist. The attempt was made from the following location:
 
    springfox.documentation.spring.web.plugins.DocumentationPluginsManager.createContextBuilder(DocumentationPluginsManager.java:154)
 
The following method did not exist:
 
    org.springframework.plugin.core.PluginRegistry.getPluginOrDefaultFor(Ljava/lang/Object;Lorg/springframework/plugin/core/Plugin;)Lorg/springframework/plugin/core/Plugin;
 
The method's class, org.springframework.plugin.core.PluginRegistry, is available from the following locations:
 
    jar:file:/C:/work/maven/mymaven/repo/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar!/org/springframework/plugin/core/PluginRegistry.class
 
It was loaded from the following location:
 
    file:/C:/work/maven/mymaven/repo/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar
 
 
Action:
 
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.plugin.core.PluginRegistry

为了解决这个问题,我们需要引入更高版本的 spring-plugin-core,就是上面 pom.xml 中最下面的两个依赖项。

做完第一步之后,我们现在需要开启 swagger,网上很多人使用的是 @EnableSwagger2WebFlux
注解,那是因为他们使用的是快照版本,现在的正式版本已经把它废弃掉了 @Deprecated,推荐的还是使用 @EnableSwagger2。

@Configuration
@EnableSwagger2
public class SwaggerWebFluxConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(new ApiInfoBuilder()
                    .description("gxf project")
                    .title("gxf RESTful APIs")
                    .termsOfServiceUrl("https://www.gxf.com/")
                    .contact(new Contact("gxf", "https://www.gxf.com/", "1333335@qq.com"))
                    .version("1.0.0")
                    .build())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.gxf.webflux.demo"))
            .paths(PathSelectors.any())
            .build()
            .genericModelSubstitutes(Optional.class, Flux.class, Mono.class);
    }
}

做完这一步之后,还不行。如果你直接运行项目,访问 swagger,会出现 404。网上很多人说需要开启 @EnableWebFlux
注解,这一步其实是多余的,加上了也还是 404。

@EnableWebFlux
@SpringBootApplication
@ComponentScan(basePackages = {"com.xttblog.webflux.demo"},
        excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
                classes = {}))
public class XttblogApplication {

    public static void main(String[] args) {
        SpringApplication.run(XttblogApplication.class, args);
    }
}

一番搜索后,我在官网上看到了 WebFluxConfigurer,通过配置实现 WebFluxConfigurer 后,可以完美解决
webflux 整合 swagger 出现 404 问题。

@Configuration
public class SwaggerUiWebFluxConfigurer implements WebFluxConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 配置跨域
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "OPTION", "DELETE").maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 重新映射路径
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);

        registry.addResourceHandler("/swagger-ui.html**")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/").resourceChain(true);
    }
}

网上有很多野代码,都是基于 3.0 的快照版本,根本就无法解决 404 问题,包括 stackoverflow 的回答。

因为 3.0.0 的正式版本中,访问路径是 http://localhost:8080/swagger-ui/index.html,且这些
swagger 资源是放在
classpath:/META-INF/resources/webjars/springfox-swagger-ui/
下面的,很多回答是把资源放在 classpath:/META-INF/resources/ 下,并且映射的是
swagger-ui.html。这是低版本的 swagger 的做法,正式版本和快照版本还是有很大差别的,官网上修复了很多 bug。

做完上面的工作后,启动项目,最终实现 weblux 和 swagger3 的整合,推荐使用正式版本的
springfox-spring-webflux。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WebFlux集成Swagger2需要使用3.0.0以上的版本。你可以在pom.xml文件中添加以下依赖项来支持WebFluxSwagger版本: ```xml <properties> <swagger.version>3.0.0</swagger.version> </properties> <!-- webflux集成swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-spring-webflux</artifactId> <version>${swagger.version}</version> </dependency> ``` 此外,你还需要在WebFlux的配置文件中添加Swagger路径的代码。你可以在WebfluxConfig类中添加以下代码: ```java @Configuration @EnableWebFlux public class WebfluxConfig implements WebFluxConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") .resourceChain(false); } } ``` 最后,在应用主类上增加@EnableOpenApi注解,并删除之前版本的SwaggerConfig.java文件。启动项目后,你可以通过访问地址http://localhost:8200/swagger-ui/index.html来查看Swagger UI界面。注意,2.x版本中访问的地址为http://localhost:8200/swagger-ui.html。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [WebFlux整合MongoDB+WebFlux集成swagger2](https://blog.csdn.net/qq_43248715/article/details/127423499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [Webflux集成Swagger3](https://blog.csdn.net/weixin_46633487/article/details/119244118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10000guo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值