SpringBoot和Springfox(Swagger)版本不兼容的解決方案
一.報錯信息
org.springframework.context.ApplicationContextException: Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
二.解決方案
根據提供的錯誤信息和搜索結果,這個問題通常與 Spring Boot 和 Springfox(Swagger)的集成有關。錯誤提示Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
表明在 Spring Boot 應用啟動過程中,documentationPluginsBootstrapper
這個 bean 無法正常啟動,原因是遇到了空指針異常(NullPointerException)。這通常是由于 Spring Boot 和 Springfox 的版本不兼容導致的路徑匹配策略沖突。
1.修改 Spring MVC 的路徑匹配策略
修改 Spring MVC 的路徑匹配策略:Springfox 假設 Spring MVC 的路徑匹配策略是ant-path-matcher,而 Spring Boot 2.6 及以上版本的默認匹配策略是path-pattern-matcher。您可以通過在application.yml或application.properties配置文件中添加以下配置來解決這個問題:
spring: mvc: pathmatch: matching-strategy: ant_path_matcher
這樣可以將 Spring MVC 的路徑匹配策略更改為ant-path-matcher
,以兼容 Springfox 的要求。
2.配置 WebMvcConfigurer
配置 WebMvcConfigurer:您可以通過創(chuàng)建一個配置類并繼承WebMvcConfigurationSupport
,然后重寫addResourceHandlers
方法來解決靜態(tài)資源路徑問題:
@Configuration public class WebMvcConfigurer extends WebMvcConfigurationSupport { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } }
這樣可以確保 Swagger 的靜態(tài)資源能夠被正確加載。
3.檢查依賴關系
檢查依賴關系:確保您的項目中包含了正確的 Spring Boot Actuator 依賴。如果您使用的是 Maven,可以在pom.xml
文件中添加以下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
這有助于確保documentationPluginsBootstrapper
bean 能夠正確創(chuàng)建。
4.降低 SpringBoot 版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> </parent>
到此這篇關于SpringBoot和Springfox(Swagger)版本不兼容的解決方案的文章就介紹到這了,更多相關SpringBoot和Springfox版本不兼容內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring MVC @GetMapping和@PostMapping注解的使用方式
這篇文章主要介紹了Spring MVC @GetMapping和@PostMapping注解的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05深入解析Maven 插件參數(shù)注入與Mojo開發(fā)
本文將深入探討Mojo開發(fā)中的參數(shù)處理機制,通過剖析@Parameter注解的實現(xiàn)原理、對比字段注入與Setter方法注入的底層差異,并結合Apache Maven 3.9.x版本的源碼解析,為讀者構建完整的插件開發(fā)知識體系,感興趣的朋友一起跟隨小編學校吧2025-05-05Java并發(fā)編程中的CyclicBarrier線程屏障詳解
這篇文章主要介紹了Java并發(fā)編程中的CyclicBarrier線程屏障詳解,2023-12-12在springboot3微項目中如何用idea批量創(chuàng)建單元測試邏輯
這篇文章主要介紹了在SpringBoot3項目中使用IntelliJIDEA批量創(chuàng)建單元測試包括準備工作(確保項目配置正確,添加測試依賴),使用IntelliJIDEA創(chuàng)建測試,感興趣的朋友一起看看吧2024-10-10IntelliJ IDEA 2021 Tomcat 8啟動亂碼問題的解決步驟
很多朋友遇到過IntelliJ IDEA 2021 Tomcat 8啟動的時候出現(xiàn)各種奇葩問題,最近有童鞋反映IntelliJ IDEA 2021 Tomcat 8啟動亂碼,正好我也遇到這個問題,下面我把解決方法分享給大家需要的朋友參考下吧2021-06-06