[Swagger] Springboot 2.6.2 + springfox 3.0.0 启动报错的解决方案

[Swagger] Springboot 2.6.2 + springfox 3.0.0 启动报错的解决方案

概述

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

环境配置

dependencies {
  ...   
 
 // swagger
 implementation 'io.springfox:springfox-boot-starter:3.0.0'
 //  WebEndpointsSupplier 包支持
 implementation 
'org.springframework.boot:spring-boot-starter-actuator:2.6.2'
// swagger 解决类型转换报错问题
implementation 'io.swagger:swagger-annotations:1.6.5'
 }

解决方案

  1. yml配置文件增加以下配置

    spring:
    # 解决swagger启动报错documentationPluginsBootstrapper的问题
      mvc:
        pathmatch:
          matching-strategy: ant_path_matcher
    
  2. 修改configuration

    
    @Configuration
    @EnableOpenApi
    public class Swagger3Configuration {
     
        ...
    
        /* 解决swagger3.0.0(springfox 3.0.0 +Spring boot 2.6.2 报错的问题 ) */
        @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));
        }
    }
    
    

官方issues

你可能感兴趣的:([Swagger] Springboot 2.6.2 + springfox 3.0.0 启动报错的解决方案)