广告
返回顶部
首页 > 资讯 > 精选 >Spring Boot 2.6.x整合Swagger启动失败报错如何解决
  • 350
分享到

Spring Boot 2.6.x整合Swagger启动失败报错如何解决

2023-06-29 09:06:30 350人浏览 薄情痞子
摘要

这篇文章主要介绍了spring Boot 2.6.x整合swagger启动失败报错如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring Boot 2.6.x

这篇文章主要介绍了spring Boot 2.6.x整合swagger启动失败报错如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring Boot 2.6.x整合Swagger启动失败报错如何解决文章都会有所收获,下面我们一起来看看吧。

    问题

    Spring Boot 2.6.x版本引入依赖 springfox-boot-starter (Swagger 3.0) 后,启动容器会报错:

    Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception…

    原因

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

    解决方案

    方案一(治标)

    在 application.properties 配置文件中修改mvc的匹配策略:

    spring.mvc.pathmatch.matching-strategy=ant-path-matcher

    注意:开始的时候我用这个方法的确可以正常启动了,但后来我发现此方法在某些服务启动时会失效!我查了一下才发现这个方法治标不治本,具体如下:

    只有在不使用 Spring Boot 的执行器时,此功能才起作用。

    无论配置的匹配策略如何,执行器将始终使用基于路径模式的解析 ( 也就是默认策略 ) 。

    如果您想在 Spring Boot 2.6及更高版本中将其与执行器一起使用,则需要对 Springfox 进行更改。

    所以解铃还须系铃人呐!要想彻底解决这个bug,需要修改的是 Springfox 。

    方案二(治本)

    这个办法是我在 GitHub 上找到的,一个大佬提了一个解决方案是将 Springfox 的某 .java 文件复制到自己项目里进行修改,另一个大佬提了一个更好的解决方案,我觉得针不戳,在这里分享一下:

    在你的项目里添加这个 bean :(加在配置类里就可)

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

    OK,启动成功!

    补充:springboot集成swagger,启动时抛出如下错误:

    03:03.586 [main] INFO  o.s.b.a.l.ConditionEvaluationReportLoggingListener -
    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    18:03:03.601 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter -

    ***************************
    APPLICATION FAILED TO START
    ***************************
    Description:
    Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 15 were found:
        - modelBuilderPluginReGIStry: defined in null
        - modelPropertyBuilderPluginRegistry: defined in null
        - typeNameProviderPluginRegistry: defined in null
        - documentationPluginRegistry: defined in null
        - apiListingBuilderPluginRegistry: defined in null
        - operationBuilderPluginRegistry: defined in null
        - parameterBuilderPluginRegistry: defined in null
        - expandedParameterBuilderPluginRegistry: defined in null
        - resourceGroupingStrategyRegistry: defined in null
        - operationModelsProviderPluginRegistry: defined in null
        - defaultsProviderPluginRegistry: defined in null
        - pathDecoratorRegistry: defined in null
        - relProviderPluginRegistry: defined by method 'relProviderPluginRegistry' in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
        - linkDiscovererRegistry: defined in null
        - entityLinksPluginRegistry: defined by method 'entityLinksPluginRegistry' in class path resource [org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.class]

    原因:

    swagger版本问题,我本地SpringBoot版本是2.3.1,引用swaggerb版本为2.2.2,导致项目启动失败

    解决方案:

    更换swagger版本,我这里换成了2.9.2版本,项目启动成功

    <dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger2</artifactId>    <version>2.9.2</version></dependency><dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger-ui</artifactId>    <version>2.9.2</version></dependency>

    关于“Spring Boot 2.6.x整合Swagger启动失败报错如何解决”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Spring Boot 2.6.x整合Swagger启动失败报错如何解决”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。

    --结束END--

    本文标题: Spring Boot 2.6.x整合Swagger启动失败报错如何解决

    本文链接: https://www.lsjlt.com/news/323954.html(转载时请注明来源链接)

    有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

    本篇文章演示代码以及资料文档资料下载

    下载Word文档到电脑,方便收藏和打印~

    下载Word文档
    猜你喜欢
    • Spring Boot 2.6.x整合Swagger启动失败报错如何解决
      这篇文章主要介绍了Spring Boot 2.6.x整合Swagger启动失败报错如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring Boot 2.6.x...
      99+
      2023-06-29
    • Spring Boot 2.6.x整合Swagger启动失败报错问题的完美解决办法
      目录问题原因解决方案方案一(治标)方案二(治本)总结问题 Spring Boot 2.6.x版本引入依赖 springfox-boot-starter (Swagger 3.0) 后...
      99+
      2022-11-13
    • 启动Spring Boot 项目失败如何解决
      启动Spring Boot 项目失败如何解决?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。Spring Boot 项目是不是经常失败,显示一大堆的错误信息,如端口重复绑定时...
      99+
      2023-06-06
    软考高级职称资格查询
    编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
    • 官方手机版

    • 微信公众号

    • 商务合作