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

在这里插入图片描述

这是因为springboot的版本和Springfox-Swagge的版本不兼容,可能会报错如下所示:

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

在这里插入图片描述

在这里插入图片描述

因为Springfox 使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X以上使用的是PathPatternMatcher。

解决方案

降低Spring Boot 版本到2.6.x以下版本

比如比较经典的版本组合

在这里插入图片描述

SpringBoot版本不降级解决方案

在这里插入图片描述

这里需要改一些配置

application.yml中添加如下配置

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

SwaggerConfig类解决兼容问题

    /**
     * 增加如下配置可解决Spring Boot 与Swagger 不兼容问题
     * @param webEndpointsSupplier
     * @param servletEndpointsSupplier
     * @param controllerEndpointsSupplier
     * @param endpointMediaTypes
     * @param corsProperties
     * @param webEndpointProperties
     * @param environment
     * @return
     */
    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List> allEndpoints = new ArrayList();
        Collection 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));
    }

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/fae5f187df.html