Failed to start bean 'documentationPluginsBootstrapper

编程教程 > Java (1056) 2024-11-26 14:39:04

spring boot 项目启动报错

Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

环境说明

  • spring boot 2.6.13
  • springfox-swagger 2.10.5

解决办法(一)

在配置类中,添加下面bean的信息

@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
    return new BeanPostProcessor() {

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof WebMvcRequestHandlerProvider) {
                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);
            }
        }
    };
}

重要提示:上面的方法虽然解决启动报错,但是由于没有修改默认的地址策略,swagger配置还需要修改才能映射出文档信息

解决办法二

2.1 修改mvc匹配策略

编辑application.yml配置文件,修改mvc的匹配策略为ANT,默认是PATH

spring:
 mvc:
  pathmatch:
    # fix swagger error
    matching-strategy: ant_path_matcher

2.2 配置类添加以下配置

/**
 * 增加如下配置可解决Spring Boot  与Swagger 3.0.0 不兼容问题
 **/
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
    List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
    Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
    allEndpoints.addAll(webEndpoints);
    allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
    allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
    String basePath = webEndpointProperties.getBasePath();
    EndpointMapping endpointMapping = new EndpointMapping(basePath);
    boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
    return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
}
private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
    return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}

以上两种方式解决spring boot 高版本和swagger不兼容问题,推荐使用方法二,能完美兼容swagger使用,不用修改其他。


评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
spring boot 项目启动报错 Failed to start bean 'documentationPluginsBootstrapper'; nested exception i...
【重要提示】:目前已有更好的方法,可以参考:Swagger2 导出离线 word文档一、使用背景    如今,REST和微服务已经有了很大的发展势头
1.前言通过前面的两篇博客Spring Boot Security Swagger2整合生成安全的在线REST API文档 SpringMVC也可参考spring boot REST 通过Swa...
Swagger2 导出离线文档简述继上一篇搬运国外的swagger2导出离线文档之后,最近发现国内一款不错的swagger ui组件和导出离线的方法,比之前的更简单优雅
问题描述 spring boot 2.6.x 项目启动报错 The elements [xxx,xxx] were left unbound 细节日志就是spring boot...
前言距离springfox的swagger2.x 以及3.0.0 长久等待,等来了springdoc的swagger 3 为啥是3是因为支持openapi3.0
升级环境说明目前项目使用的2.3.7版本(自己感觉还行,但是官方已经停止支持了。)Spring Boot 官方支持情况spring boot 官方支持情况官方在今年8月就终止了对2.3.x的版本...
Swagger Spring Boot Stater简介spring boot 整合 springfox 实现swagger api文档生成
swagger dataType 有那些2.6.x"int", "date", "string", "double", "float", "boolean", "byte", "object",...
springfox swagger 请求参数类型设置解析类:springfox.documentation.spring.web.readers.parameter.ParameterTypeR...
创建REST API时,良好的文档是有帮助的。而且,API中的每一个变化都应该在参考文档中同时描述。手动完成这是一个乏味的操作,因此这个过程的自动化是不可避免的。
错误信息 spring cloud 启动报错 No spring.config.import property has been defined 环境说明 spring ...
Spring Boot logback tomcat部署不生效问题解决
环境说明 之前spring cloud 用的基于spring boot 2.2.x版本,sleuth版本同步为2.x 现在升级了spring cloud 版本,基于了spring b...
eclipse启动报错"reload maven project' has encountered a proble" 解决方法,怎么办?