服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring Boot 2.6.x整合Swagger启动失败报错问题的完美解决办法

Spring Boot 2.6.x整合Swagger启动失败报错问题的完美解决办法

2022-08-24 11:32toollong Java教程

这篇文章主要给大家介绍了关于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 :(加在配置类里就可)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@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);
            }
        }
    };
}

OK,启动成功!

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

18: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版本,项目启动成功

?
1
2
3
4
5
6
7
8
9
10
<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启动失败内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_49523761/article/details/122305980

延伸 · 阅读

精彩推荐