org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
## 一、报错信息
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
具体报错信息如下:

## 二、报错原因
SpringBoot2.6.x与Swagger2 3.0.0版本冲突原因。
## 三、 解决方案
方案一:
将SpringBoot版本降低到2.6以下。
方案二:
- 在application.yml中添加以下配置:

Swagger配置类
如上,更改SpringMVC处理程序映射匹配请求路径策略为ant_path_matcher不起作用,可以Spring容器中注册一个BeanPostProcessor,在postProcessAfterInitialization中修改WebMvcRequestHandlerProvider中的handlerMappings属性。
@Configuration
@EnableOpenApi
@Slf4j
public class SwaggerConfig implements ApplicationListener<WebServerInitializedEvent> {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.www.wen"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo getApiInfo(){
return new ApiInfoBuilder()
.title("xxx应用平台") // 文档标题
.description("xxx应用平台文档描述") // 文档描述
.termsOfServiceUrl("http://www.baidu.com")
.version("1.0")
.build();
}
/**
* 解决SpringBoot和Swagger2冲突
*
* @return
*/
@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
}
return bean;
}
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream()
.filter(mapping -> mapping.getPatternParser() == null)
.collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}
@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
log.warn("修改WebMvcRequestHandlerProvider的属性:handlerMappings出错,可能导致swagger不可用", e);
throw new IllegalStateException(e);
}
}
};
}
}

浙公网安备 33010602011771号