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