SpringBoot项目启动报错Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException

前言:新项目搭建使用的spring-boot-starter-parent的2.6.2版本(高于2.5.x的版本),在启动时报出这个错误。
原因:springboot 集成Swagger2报错
  • 查阅这个问题的相关博客之后,找到两个解决方法,这里仅作交流记录。
方式一:
试错:

项目中使用的Swagger版本是2.9.2、springboot 版本是2.6.2(高于2.5.x的版本)

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> 
    </parent>

springboot版本太高,缺少swagger运行所需要的环境,添加

        <!--        Swagger所需依赖:guava-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>25.1-jre</version>
        </dependency>

依然报错。

解决:

将spring boot版本回退到2.5.7或2.5.6(个人认为2.5.x都可,但是我只试了这两个版本),项目可正常启动。

	<!-- spring boot 2.5.7 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
方式二:

在启动类加一个注解:@EnableWebMvc
启动类页面
启动成功。
启动成功页面


(方式三):

项目整合了Spring boot 2.6.6和springfox-swagger2 2.9.2,启动时按照以上两个方法修改之后,依然出现上述问题。依赖如下:

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>swagger-annotations</artifactId>
                    <groupId>io.swagger</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>swagger-models</artifactId>
                    <groupId>io.swagger</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>


        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
   </dependencies>
解决:

在项目的yml文件里,加上如下配置:

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

参考博客:
https://blog.csdn.net/weixin_44034837/article/details/121586195
https://truedei.blog.csdn.net/article/details/109861077
https://blog.csdn.net/qq_42042158/article/details/121686751
https://www.cnblogs.com/rainbow70626/p/15680184.html

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐