SpringBoot2.6.x集成swagger: Failed to start bean ‘documentationPluginsBootstrapper问题解决

在SpringBoot2.6.x版本中集成Swagger时遇到Failed to start bean 'documentationPluginsBootstrapper'的空指针异常。问题主要由SpringBoot更新的PathPatternMatcher与Swagger的AntPathMatcher不兼容引起。解决办法是在配置文件中设置spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER,或者创建一个配置类并使用BeanPostProcessor来定制Springfox的HandlerMappings。如果问题依然存在,可以参考相关文章获取更多集成步骤和问题解决方法。

在使用SpringBoot2.6.x集成swagger的时候,启动的时候出现了:Failed to start bean 'documentationPluginsBootstrapper 的空指针异常。

在网上查询解决方案,有的说是guava的版本过低导致的,升级了一下guava版本问题依旧存在。还有的说是添加一个注解 @EnableWebMvc的,这个注解确实管用,但是要注意,尽量不要使用这个注解,这个注解会导致SpringMVC的自动化配置失效,有可能出现其他的问题。

这个问题的主要原因确实是SpringBoot版本过高导致。如果你用的是SpringBoot2.5.x及之前版本是没有问题的。

Spring Boot 2.6.X使用PathPatternMatcher匹配路径,Swagger引用的Springfox使用的路径匹配是基于AntPathMatcher的。

所以要想解决,添加配置,将springBoot MVC的路劲匹配模式修改一下即可。

在springBoot配置文件中添加配置:

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
 

如果是yml格式的配置文件,则需要修改一下格式:

如果在集成过程中出现了其他的问题,可以参考一下我之前写过的文章,里边介绍了集成的详细步骤及问题解决。

如果加了上述代码还是不管用,那么再添加一个配置 类。

package cn.xxx.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qingf
 * @title KnifeConfig
 * @description
 * @create 2024/4/22
 */

@Configuration
@EnableSwagger2WebMvc
public class KnifeConfig {


    @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);
                }
            }
        };
    }
}



SpringBoot教程(十六) | SpringBoot集成swagger(全网最全)_一缕82年的清风的博客-优快云博客一. 接口文档概述swagger是当下比较流行的实时接口文文档生成工具。接口文档是当前前后端分离项目中必不可少的工具,在前后端开发之前,后端要先出接口文档,前端根据接口文档来进行项目的开发,双方开发结束后在进行联调测试。所以接口文档其实就是开发之前双方之间的一种约定。通常接口文档分为离线的和实时的。离线的接口文档工具有: word(相当于没说), YAPI, 小幺鸡等,这种文档需要程序员在上面编写,也一般具备接口测试功能。通常是由开发人员先在离线接口文档上编写信息,然后交给前端人员参照开发。最大的弊https://blog.youkuaiyun.com/lsqingfeng/article/details/123678701?spm=1001.2014.3001.5501

<think>嗯,我现在遇到了Spring Boot应用启动时抛出documentationPluginsBootstrapper bean的NullPointerException问题,需要解决。首先,我得回忆一下这个异常通常在什么情况下出现。可能和Springfox或者Spring Doc有关,这两个库用于生成API文档,比如Swagger。 我记得在Spring Boot应用中,如果依赖版本不兼容,可能会导致某些bean初始化失败。比如,Spring Boot 3.x与Springfox可能不兼容,因为Springfox很久没更新了。这时候可能需要换成Spring Doc。但用户的问题里提到的是documentationPluginsBootstrapper,这个类属于Springfox的Docket配置,所以可能他们还在使用Springfox。 接下来,我应该考虑错误的原因。NullPointerException通常是因为某个对象没有被正确注入或者初始化。根据引用[2],<context:annotation-config/>只在当前的应用上下文中查找注解,如果配置不正确,可能导致某些bean没有被扫描到。但用户使用的是Spring Boot,可能配置方式不同,但可能存在组件扫描的问题。 另外,引用[1]中提到使用@Value来访问属性,如果属性配置不正确,可能导致某些依赖的配置缺失,进而导致bean初始化失败。但这里的问题更可能和依赖库版本有关。 用户的应用可能引入了Springfox(如springfox-boot-starter),而Spring Boot版本较高,比如3.x,导致不兼容。解决方法可能是排除Springfox,改用Spring Doc,或者降级Spring Boot版本。例如,Spring Boot 2.6.x之后对Springfox的支持有问题,因为路径匹配策略的默认值改变了,需要手动设置spring.mvc.pathmatch.matching-strategy为ant_path_matcher。 另外,检查是否有缺少必要的配置,比如Docket的Bean定义是否正确,或者是否有冲突的依赖。此外,如果使用了Spring Security,可能需要配置权限以允许访问Swagger资源,但这可能不会直接导致NullPointerException。 总结可能的解决步骤:1. 检查Spring Boot和Springfox的版本兼容性;2. 替换Springfox为Spring Doc;3. 调整路径匹配策略;4. 检查组件扫描是否正确;5. 确保正确配置Swagger的Docket。</think>### Spring Boot启动时documentationPluginsBootstrapper NullPointerException解决方案 #### 1. 问题根本原因 该异常通常由以下原因导致: - **版本不兼容**:Spring Boot 2.6+与Springfox 3.x存在兼容性问题[^1] - **路径匹配策略变更**:Spring Boot 2.6+默认使用`PathPatternParser`替代`AntPathMatcher`[^2] - **Swagger配置缺失**:未正确配置Docket或未启用注解扫描 #### 2. 具体解决方案 **方案1:调整路径匹配策略** ```properties # application.properties spring.mvc.pathmatch.matching-strategy=ant_path_matcher ``` **方案2:升级到SpringDoc(推荐)** ```xml <!-- pom.xml --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.7.0</version> </dependency> ``` **方案3:兼容性配置(仅限Spring Boot 2.6以下)** ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` #### 3. 验证步骤 1. 检查`@SpringBootApplication`注解是否包含组件扫描 2. 确认依赖树无版本冲突(使用`mvn dependency:tree`) 3. 验证Swagger UI访问地址:`http://localhost:8080/swagger-ui.html` #### 4. 注意事项 - Spring Boot 3.x+必须使用SpringDoc,不再支持Springfox[^3] - 若使用Spring Security需配置白名单 - 确保项目结构符合标准Maven/Gradle布局
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值