解决Failed to start bean 'documentationPluginsBootstrapper’报错

在之前springboot整和knife4j的时候就出现了这个错误

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

截图如下:
在这里插入图片描述
上次使用在springboot的application.yaml文件中修改默认的路径匹配器配置解决了这个报错

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

但是今天在整合activiti的时候又出现了同样的报错,之前在yml文件中的配置好像不起作用了,我又去查别的解决方法,在一篇大佬的博客下找到了解决方法

在配置文件中注入一个Bean

/**
 * 解决springboot整合knife4j的问题
 * @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) {
                throw new IllegalStateException(e);
            }
        }
    };
}

我是放在了knife4j的配置文件Knife4jConfig中
只要是加了@Configuration的配置类都可以,把这个bean注册到spring容器中

大佬的博客:https://blog.csdn.net/zqlwcx/article/details/126729848

里面讲解了三种解决方案,我用第三种方案解决了这个问题

Logo

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

更多推荐