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)境搭建
重要:修改配置文件!修改配置文件!修改配置文件!重要的事說三遍!?。?/p>
# 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)文章
Elasticsearch?percolate?查詢示例詳解
這篇文章主要為大家介紹了Elasticsearch?percolate?查詢示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Spring中@RestController和@Controller的使用及區(qū)別
這篇文章主要介紹了Spring中@RestController和@Controller的使用及區(qū)別,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
Spring?Boot中優(yōu)雅地處理參數(shù)傳遞的技巧分享
最近一直在學(xué)習(xí)Spring Boot,今天將其中的從前臺過來的參數(shù)傳遞總結(jié)一下,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中優(yōu)雅地處理參數(shù)傳遞的技巧,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05
@Transaction,@Async在同一個類中注解失效的原因分析及解決
這篇文章主要介紹了@Transaction,@Async在同一個類中注解失效的原因分析及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot微服務(wù)注冊分布式Consul的詳細過程
這篇文章主要介紹了SpringBoot(微服務(wù))注冊分布式Consul,Spring Boot應(yīng)用可以通過向Consul注冊自身來實現(xiàn)服務(wù)發(fā)現(xiàn)和治理,使得其他服務(wù)可以在Consul中發(fā)現(xiàn)并調(diào)用它,需要的朋友可以參考下2023-04-04

