前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【WEB系列】高版本SpringBoot整合Swagger启动异常问题

【WEB系列】高版本SpringBoot整合Swagger启动异常问题

作者头像
框架师
发布2022-05-14 09:11:54
2.2K0
发布2022-05-14 09:11:54
举报
文章被收录于专栏:墨白的Java基地

前言:

Spring Boot 2.6.x 版本引入依赖 springfox-boot-starter (Swagger 3.0) 后,启动项目会报错: Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception… 异常信息: Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-05-13 11:34:35.770 [main] ERROR org.springframework.boot.SpringApplication - Application run failed org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

原因:

Springfox 假设 Spring MVC 的路径匹配策略是 ant-path-matcher,而 Spring Boot 2.6.x版本的默认匹配策略是 path-pattern-matcher,这就造成了上面的报错。

解决方案

  1. 第一种,添加配置类

继承 WebMvcConfigurationSupport 重写 addResourceHandlers 方法。

  • 编写配置类
代码语言:javascript
复制
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * software:IntelliJ IDEA 2022.1
 * class name: WebMvcConfig
 * class description: web 配置类
 *
 * @author MoBaiJun 2022/5/13 11:25
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 解决 org.springframework.context.ApplicationContextException: Failed to start bean <br>
     * 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
     * 发现如果继承了 WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
     * @param registry ResourceHandlerRegistry
     * @see <a href="https://www.mobaijun.com/posts/3051425539.html"></a>
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}
  1. 第二种,修改配置文件(我没有尝试过)
代码语言:javascript
复制
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

需要注意的是:这种方法无法彻底解决问题,只有在不使用 Spring Boot 的执行器时,此功能才起作用。 无论配置的匹配策略如何,执行器将始终使用基于路径模式的解析 ( 也就是默认策略 ) 。 如果你想在 Spring Boot 2.6 及更高版本中将其与执行器一起使用,则需要对 Springfox 进行更改。 这个办法是我在 github 上找到的,一个大佬提了一个解决方案是将 Springfox 的某 .java 文件复制到自己项目里进行修改,另一个大佬提了一个更好的解决方案,我觉得针不戳,在这里分享一下: 在你的项目里添加这个 bean :(加在配置类里就可)Spring Boot 2.6.x整合Swagger启动失败报错问题解决(治标还治本)_toollong的博客-CSDN博客

代码语言:javascript
复制
@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) {
                throw new IllegalStateException(e);
            }
        }
    };
}

参考文章: java - Failed to start bean ‘documentationPluginsBootstrapper’ in spring data rest - Stack Overflow Spring Boot 2.6.x整合Swagger启动失败报错问题解决(治标还治本)_toollong的博客-CSDN博客

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言:
  • 原因:
  • 解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档