Spring?Boot?2.6.x整合Swagger啟動(dòng)失敗報(bào)錯(cuò)問題的完美解決辦法
問題
Spring Boot 2.6.x版本引入依賴 springfox-boot-starter (Swagger 3.0) 后,啟動(dòng)容器會(huì)報(bào)錯(cuò):
Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception…
原因
Springfox 假設(shè) Spring MVC 的路徑匹配策略是 ant-path-matcher,而 Spring Boot 2.6.x版本的默認(rèn)匹配策略是 path-pattern-matcher,這就造成了上面的報(bào)錯(cuò)。
解決方案
方案一(治標(biāo))
在 application.properties 配置文件中修改mvc的匹配策略:
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
注意:開始的時(shí)候我用這個(gè)方法的確可以正常啟動(dòng)了,但后來(lái)我發(fā)現(xiàn)此方法在某些服務(wù)啟動(dòng)時(shí)會(huì)失效!我查了一下才發(fā)現(xiàn)這個(gè)方法治標(biāo)不治本,具體如下:
只有在不使用 Spring Boot 的執(zhí)行器時(shí),此功能才起作用。
無(wú)論配置的匹配策略如何,執(zhí)行器將始終使用基于路徑模式的解析 ( 也就是默認(rèn)策略 ) 。
如果您想在 Spring Boot 2.6及更高版本中將其與執(zhí)行器一起使用,則需要對(duì) Springfox 進(jìn)行更改。
所以解鈴還須系鈴人吶!要想徹底解決這個(gè)bug,需要修改的是 Springfox 。
方案二(治本)
這個(gè)辦法是我在 github 上找到的,一個(gè)大佬提了一個(gè)解決方案是將 Springfox 的某 .java 文件復(fù)制到自己項(xiàng)目里進(jìn)行修改,另一個(gè)大佬提了一個(gè)更好的解決方案,我覺得針不戳,在這里分享一下:
在你的項(xiàng)目里添加這個(gè) 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,啟動(dòng)成功!
補(bǔ)充:springboot集成swagger,啟動(dòng)時(shí)拋出如下錯(cuò)誤:
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,導(dǎo)致項(xiàng)目啟動(dòng)失敗
解決方案:
更換swagger版本,我這里換成了2.9.2版本,項(xiàng)目啟動(dòng)成功
<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>
總結(jié)
到此這篇關(guān)于Spring Boot 2.6.x整合Swagger啟動(dòng)失敗報(bào)錯(cuò)問題的完美解決辦法的文章就介紹到這了,更多相關(guān)Spring Boot 2.6.x整合Swagger啟動(dòng)失敗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot3.1.2 引入Swagger報(bào)錯(cuò)Type javax.servlet.http.HttpServletRequest not present解決辦法
- Springboot整合Swagger2后訪問swagger-ui.html 404報(bào)錯(cuò)問題解決方案
- SpringBoot引入swagger報(bào)錯(cuò)處理的解決方法
- Swagger2配置方式(解決404報(bào)錯(cuò))
- 關(guān)于springboot集成swagger3時(shí)spring-plugin-core報(bào)錯(cuò)的問題
- 解決swagger主頁(yè)訪問,返回報(bào)錯(cuò)500問題
相關(guān)文章
Java打包之后讀取Resources下的文件失效原因及解決方法
這篇文章主要給大家介紹了Java打包之后讀取Resources下的文件失效的問題分析和解決方法,文中通過代碼示例和圖文結(jié)合給大家講解非常詳細(xì),需要的朋友可以參考下2023-12-12
java實(shí)現(xiàn)轉(zhuǎn)圈打印矩陣算法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)轉(zhuǎn)圈打印矩陣算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
關(guān)于eclipse安裝spring插件報(bào)錯(cuò)An error occurred while collecting item
這篇文章主要介紹了關(guān)于eclipse安裝spring插件報(bào)錯(cuò)An error occurred while collecting items to be installed...解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
使用Spring的攔截器監(jiān)測(cè)每個(gè)Controller或方法的執(zhí)行時(shí)長(zhǎng)
這篇文章主要介紹了使用Spring的攔截器監(jiān)測(cè)每個(gè)Controller或方法的執(zhí)行時(shí)長(zhǎng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
詳解Java8合并兩個(gè)Map中元素的正確姿勢(shì)
這篇文章主要介紹了詳解Java8合并兩個(gè)Map中元素的正確姿勢(shì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(逆波蘭表達(dá)式)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,逆波蘭表達(dá)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Java基礎(chǔ)MAC系統(tǒng)下IDEA連接MYSQL數(shù)據(jù)庫(kù)JDBC過程
最近一直在學(xué)習(xí)web項(xiàng)目,當(dāng)然也會(huì)涉及與數(shù)據(jù)庫(kù)的連接這塊,這里就總結(jié)一下在IDEA中如何進(jìn)行MySQL數(shù)據(jù)庫(kù)的連接,這里提一下我的電腦是MAC系統(tǒng),使用的編碼軟件是IDEA,數(shù)據(jù)庫(kù)是MySQL2021-09-09

