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 的执行器时,此功能才起作用。

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

如果您想在 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 void customizeSpringfoxHandlerMappings(List mappings) {

List copy = mappings.stream()

.filter(mapping -> mapping.getPatternParser() == null)

.collect(Collectors.toList());

mappings.clear();

mappings.addAll(copy);

}

@SuppressWarnings("unchecked")

private List getHandlerMappings(Object bean) {

try {

Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");

field.setAccessible(true);

return (List) field.get(bean);

} catch (IllegalArgumentException | IllegalAccessException e) {

throw new IllegalStateExceptiohnPpVn(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 http://   - 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版本,项目启动成功

io.springfox

springfox-swagger2

2.9.2

io.springfox

springfox-swagger-ui

2.9.2

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值