springboot2.6+整合swagger,出现‘documentationPluginsBootstrapper‘这种错误

本文介绍了如何在SpringBoot项目中整合Swagger2,包括添加必要的maven依赖,配置Swagger2的配置类以设定API信息,以及解决Springboot2.6后的路径匹配问题。通过这些步骤,可以方便地为应用构建RESTfulAPI的文档。
摘要由CSDN通过智能技术生成

Spring Boot 整合 Swagger2
    1.添加pom依赖
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.webjars/bootstrap -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>5.1.3</version>
        </dependency>
        
    2.添加swagger的配置类
        @Configuration
        //@EnableWebMvc
        @EnableSwagger2
        public class Swagger2 {

            // swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
            @Bean // 创建一个bean
            public Docket createRestApi() {
                return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                        // 为当前包路径
                        .apis(RequestHandlerSelectors.basePackage("com.etc.ssm.controller")).paths(PathSelectors.any()).build();
            }

            // 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
            private ApiInfo apiInfo() {
                return new ApiInfoBuilder()
                        // 页面标题
                        .title("Spring  测试使用 Swagger2 构建RESTful API")
                        // 创建人
                        .contact(new Contact("小白", "http://chinasofti.com", ""))
                        // 版本号
                        .version("1.0")
                        // 描述
                        .description("API 描述").build();
            }
        }
        
    3.因为: Springboot2.6以后将SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser,导致出错
                会出现这个错误:org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper';
    
            在配置文件application.properties中加入以下配置
            spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

            如果是application.yml就加入
                spring:
                  mvc:
                    pathmatch:
                      matching-strategy: ant_path_matcher

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值