本人亲测好用!!!

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

2、报错原因:SpringBoot 与Swagger版本冲突

3、解决方案:

可以先试试这个:

引别的包依赖的时候可能会报这个错误,可以试着排除

方案一:

        将SpringBoot版本降低到2.6以下。

方案二:

        在application.yml中添加以下配置:

  spring:
    mvc:
      pathmatch:
        matching-strategy: ant_path_matcher

Swagger配置类
更改SpringMVC处理程序映射匹配请求路径策略为ant_path_matcher不起作用,可以Spring容器中注册一个BeanPostProcessor,在postProcessAfterInitialization中修改WebMvcRequestHandlerProvider中的handlerMappings属性。

@Configuration
@EnableOpenApi
@Slf4j
public class SwaggerConfig implements ApplicationListener<WebServerInitializedEvent> {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.org"))
                .paths(PathSelectors.any())
                .build();
    }
  
    public ApiInfo getApiInfo(){
        return new ApiInfoBuilder()
                .title("xxx应用平台") // 文档标题
                .description("xxx应用平台文档描述") // 文档描述
                .termsOfServiceUrl("http://www.baidu.com")
                .version("1.0")
                .build();
  
    }
     
    /**
     * 解决SpringBoot和Swagger2冲突
     *
     * @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) {
                    log.warn("修改WebMvcRequestHandlerProvider的属性:handlerMappings出错,可能导致swagger不可用", e);
                    throw new IllegalStateException(e);
                }
            }
        };
    }
}

或者找找自己的项目里面有没有Swagger2Config或者SwaggerConfig配置类

把这个注释掉即可

@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐