Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerEx

一、报错问题

报错信息:
Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
报错信息

二、问题发生背景

Spring Boot 整合 Swagger,用于生成 Web API 文档。

版本信息:spring boot:2.7.6,swagger:2.8.0

三、原因分析

springboot 升级到 2.6.0之后,swagger版本和springboot出现了不兼容情况,因为SpringBoot处理映射匹配的默认策略发生变化:

请求路径与 Spring MVC 处理映射匹配的默认策略已从AntPathMatcher更改为PathPatternParser

1. springboot 2.6.0之前:

public static class Pathmatch {
	private MatchingStrategy matchingStrategy = MatchingStrategy.ANT_PATH_MATCHER;
}

2.springboot2.6.0之后

public static class Pathmatch {
    private MatchingStrategy matchingStrategy = MatchingStrategy.PATH_PATTERN_PARSER;
}

四、解决方案

方案一:修改配置文件(最优,亲测

升级springboot到2.6之后,需要修改配置文件来兼容Swagger2
application.properties:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

application.yml:

spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher

方案二:统筹规划SpringBoot和Swagger版本

swagger版本问题,升级3.0版本

或者

降低Spring Boot 版本,比如可以考虑将Spring Boot版本降低为2.5.6