Springboot2.7整合knife4j-openapi2-spring-boot报错Failed to start bean ‘documentationPluginsBootstrapper

最近在用Springboot2.7整合knife4j-openapi2-spring-boot-starter后,发现无法启动项目直接报错

报错信息

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

网上解决办法

当时直接搜索引擎一动,解决办法基本上都是添加一个配置。原因是:Spring Boot 2.6以上引入的新PathPatternParser,需要进行配置,不然整合起来会报错

spring.mvc.pathmatch.matching-strategy=ant-path-matcher 

具体问题原因和解决方案

当时以为加上就可以了,结果发现并没有解决问题,问题还是存在,最后网上再次搜索验证,得到的问题原因是和项目引入的其他依赖冲突,当项目当中引入了spring-boot-starter-actuator就会出现这个问题。

可用的解决方案是添加一个bean配置:

	@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) {
	            mappings.removeIf(mapping -> mapping.getPatternParser() != null);
	        }
	
	        @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) {
	                throw new IllegalStateException(e);
	            }
	        }
	    };
	}

最后附上解决资料连接:https://gitee.com/xiaoym/knife4j/issues/I4JT89 (没事多看看issues,大佬云集)

### 解决 `Failed to start bean documentationPluginsBootstrapper` 的 NullPointerException 问题 当遇到 `Failed to start bean documentationPluginsBootstrapper` 并伴随有 `NullPointerException` 错误时,通常是因为 Spring Boot 应用程序在初始化 Knife4j 或者 Swagger 文档插件的过程中遇到了某些未预期的情况。具体原因可能涉及路径匹配策略不兼容等问题。 #### 修改路径匹配策略 为了使 Knife4jSpring Boot 更好地协同工作,可以调整应用程序中的路径匹配策略设置。通过指定 `spring.mvc.pathmatch.matching-strategy=ant_path_matcher` 可以解决一些由默认路径解析器引起的冲突[^5]: ```yaml spring: mvc: pathmatch: matching-strategy: ant_path_matcher ``` 此配置项的作用在于改变 URL 路径模式匹配的行为方式,使得其更符合传统风格的 Ant 风格通配符语法,从而减少潜在的错误发生几率。 #### 更新依赖版本 确保所使用的库是最新的稳定版也很重要。对于 Spring Boot 版本较高的项目来说,建议升级到与之相适应的新版本 Knife4j/Swagger UI 组件,因为旧版本可能存在已知缺陷或不再支持新特性[^4]。 #### 检查自定义配置类 如果存在自定义的Swagger配置类(如 `@Configuration` 注解标注),则需确认这些类内的逻辑不会导致 NPE 发生。例如,在创建 OpenAPI 对象实例之前应先验证必要的参数是否为空[^3]: ```kotlin @Configuration class SwaggerConfig { @Bean fun customOpenApi(): OpenAPI? { val info = Info() .title("My API Title") .version("1.0") // Ensure all required fields are set before returning the object. if (info.title != null && info.version != null) { return OpenAPI().info(info) } throw IllegalStateException("Invalid configuration detected.") } } ``` 上述代码片段展示了如何安全构建 OpenAPI 实例并处理可能出现的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值