SpringBoot和Springfox(Swagger)版本不兼容的解決方案
一.報錯信息
org.springframework.context.ApplicationContextException: Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
二.解決方案
根據(jù)提供的錯誤信息和搜索結(jié)果,這個問題通常與 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版本不兼容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot如何通過Feign調(diào)用傳遞Header中參數(shù)
這篇文章主要介紹了SpringBoot通過Feign調(diào)用傳遞Header中參數(shù),本文給大家分享兩種解決方案給大家詳細講解,需要的朋友可以參考下2023-04-04
IntelliJ IDEA下Maven創(chuàng)建Scala項目的方法步驟
這篇文章主要介紹了IntelliJ IDEA下Maven創(chuàng)建Scala項目的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06
Spring?Boot攔截器Interceptor與過濾器Filter實戰(zhàn)指南
本文給大家介紹Spring?Boot攔截器Interceptor與過濾器Filter實戰(zhàn)指南,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-05-05

