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 :(加在配置类里就可)
@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版本,项目启动成功
<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启动失败内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java打包之后读取Resources下的文件失效原因及解决方法
这篇文章主要给大家介绍了Java打包之后读取Resources下的文件失效的问题分析和解决方法,文中通过代码示例和图文结合给大家讲解非常详细,需要的朋友可以参考下2023-12-12
关于eclipse安装spring插件报错An error occurred while collecting item
这篇文章主要介绍了关于eclipse安装spring插件报错An error occurred while collecting items to be installed...解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-08-08
使用Spring的拦截器监测每个Controller或方法的执行时长
这篇文章主要介绍了使用Spring的拦截器监测每个Controller或方法的执行时长,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-10-10
Java基础MAC系统下IDEA连接MYSQL数据库JDBC过程
最近一直在学习web项目,当然也会涉及与数据库的连接这块,这里就总结一下在IDEA中如何进行MySQL数据库的连接,这里提一下我的电脑是MAC系统,使用的编码软件是IDEA,数据库是MySQL2021-09-09


最新评论