SpringBoot任意版本集成Swagger各種版本的操作指南
引言:喜歡自學(xué)的新手們,在學(xué)習(xí)Swagger生成API文檔的時候經(jīng)常會遇到問題,而目前市面上大部分技術(shù)分享者的SpringBoot版本并沒和我們的同步,導(dǎo)致一些一模一樣的代碼,在我們的項目上卻無法使用,這是一個經(jīng)常性的問題,本文章就旨在和大家列舉幾種常用的項目搭配,來解決訪問網(wǎng)址:http://localhost:8080/swagger-ui/index.html 報錯“Error Page”的問題。
首先聲明一下使用 Spring Boot 2.7 及以上版本時,Swagger 2.9.2會有一些兼容性問題,很有可能在項目運行前我們就飲恨于此了。
比較常見的幾個錯誤:
1、“org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException”
2、“Failed to start bean 'documentationPluginsBootstrapper'
3、“Caused by: java.lang.NullPointerException: null”。
簡單的解決辦法:
版本對應(yīng)(Maven官方倉庫:https://mvnrepository.com/)
①SPB(SpringBoot) 2.7 以下 + Swagger-ui 和Swagger2 2.9.2
②SPB(SpringBoot) 2.7 往上 + springfox-boot-starter(3.0.0包括Swagger-ui 和Swagger2 3.0.0)
直接改用Springdoc OpenAPI
補充性說明
一、低版本過程性搭建
1、依賴展示
<properties> //……省略 <spring-boot.version>2.5.3</spring-boot.version> </properties> //……省略 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <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>
2、配置Swagger
(1)最小環(huán)境搭建
*注意低版本url:http://localhost:8080/swagger-ui.html
*注意高版本url:http://localhost:8080/swagger-ui/index.html
???????import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @Configuration // 注入spring boot public class SwaggerConfig { } // 只用這幾行代碼就可以運行
二、高版本過程性搭建
1、依賴展示
//……省略 <spring-boot.version>2.7.6</spring-boot.version> // ……省略 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2、配置Swagger
(1)最小環(huán)境搭建
重要:修改配置文件!修改配置文件!修改配置文件!重要的事說三遍!!!
# application.properties寫法 # Spring Boot 2.6.X版本引入了新的路徑匹配策略,這導(dǎo)致了與Springfox的不兼容。 # Spring Boot使用PathPatternMatcher作為默認的路徑匹配策略,而Springfox依賴于 # AntPathMatcher。所以做以下修改: spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
*注意低版本url:http://localhost:8080/swagger-ui.html
*注意高版本url:http://localhost:8080/swagger-ui/index.html
import org.springframework.context.annotation.Configuration; import springfox.documentation.oas.annotations.EnableOpenApi; @EnableOpenApi // config文件只改變了這里哦 @Configuration // 注入spring boot public class SwaggerConfig { }
三、Springdoc OpenAPI高低環(huán)境搭建
得多皮才非得高配低、低配高?。课铱蓻]這閑工夫再寫了,上面兩種解決方法自己排列組合試去吧
四、配置Swagger
以上所有SwaggerConfig配置類都是默認情況,下面簡單分享一下自定義配置項。
@EnableOpenApi @Configuration // 注入spring boot public class SwaggerConfig { @Bean // 要想配置生效必須注入 public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot2.7.6中使用Swagger3.0.0構(gòu)建RESTful APIs") .description("我可是描述信息哈~~更多Spring Boot相關(guān)文章請關(guān)注:http://blog.didispace.com/") .version("8.0") .build(); } }
以上就是SpringBoot任意版本集成Swagger各種版本的操作指南的詳細內(nèi)容,更多關(guān)于SpringBoot集成Swagger的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot項目中java -jar xxx.jar沒有主清單屬性的解決方法
這篇文章主要給大家介紹了SpringBoot項目中java -jar xxx.jar沒有主清單的解決方法,文中通過代碼示例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01Java concurrency之非公平鎖_動力節(jié)點Java學(xué)院整理
本篇文章主要介紹了Java concurrency之非公平鎖,詳細的介紹了獲取和釋放非公平鎖,有興趣的同學(xué)可以了解一下2017-06-06Java?Web實戰(zhàn)之使用三層架構(gòu)與Servlet構(gòu)建登錄注冊模塊
這篇文章介紹了如何使用三層架構(gòu)(View,?Service,?DAO)和JDBCTemplate技術(shù)在JavaWeb環(huán)境下實現(xiàn)登錄和注冊功能,詳細說明了構(gòu)建項目的步驟,包括創(chuàng)建數(shù)據(jù)庫表、實體類、DAO層、Service層、Servlet處理及頁面設(shè)計,需要的朋友可以參考下2024-10-10使用Spring的StopWatch實現(xiàn)代碼性能監(jiān)控的方法詳解
在開發(fā)過程中,偶爾還是需要分析代碼的執(zhí)行時間,Spring 框架提供了一個方便的工具類 StopWatch,本文將介紹 StopWatch 的基本用法,并通過示例演示如何在項目中使用 StopWatch 進行代碼性能監(jiān)控2023-12-12java8使用流的filter來篩選數(shù)據(jù)的實現(xiàn)
這篇文章主要介紹了java8使用流的filter來篩選數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java實現(xiàn)bmp和jpeg圖片格式互轉(zhuǎn)
本文主要介紹了Java實現(xiàn)bmp和jpeg圖片格式互轉(zhuǎn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Spring Core動態(tài)代理的實現(xiàn)代碼
通過JDK的Proxy方式或者CGLIB方式生成代理對象的時候,相關(guān)的攔截器已經(jīng)配置到代理對象中去了,接下來通過本文給大家介紹Spring Core動態(tài)代理的相關(guān)知識,需要的朋友可以參考下2021-10-10完美解決springboot中使用mybatis字段不能進行自動映射的問題
今天在springboot中使用mybatis的時候不能字段不能夠進行自動映射,接下來給大家給帶來了完美解決springboot中使用mybatis字段不能進行自動映射的問題,需要的朋友可以參考下2023-05-05