欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot任意版本集成Swagger各種版本的操作指南

 更新時(shí)間:2024年07月11日 11:09:58   作者:??????鯨落  
在學(xué)習(xí)Swagger生成API文檔的時(shí)候經(jīng)常會(huì)遇到問題,而目前市面上大部分技術(shù)分享者的SpringBoot版本并沒和我們的同步,導(dǎo)致一些一模一樣的代碼,在我們的項(xiàng)目上卻無法使用,這是一個(gè)經(jīng)常性的問題,本文章就旨在和大家搞定SpringBoot任意版本集成Swagger各種版本

引言:喜歡自學(xué)的新手們,在學(xué)習(xí)Swagger生成API文檔的時(shí)候經(jīng)常會(huì)遇到問題,而目前市面上大部分技術(shù)分享者的SpringBoot版本并沒和我們的同步,導(dǎo)致一些一模一樣的代碼,在我們的項(xiàng)目上卻無法使用,這是一個(gè)經(jīng)常性的問題,本文章就旨在和大家列舉幾種常用的項(xiàng)目搭配,來解決訪問網(wǎng)址:http://localhost:8080/swagger-ui/index.html 報(bào)錯(cuò)“Error Page”的問題。

首先聲明一下使用 Spring Boot 2.7 及以上版本時(shí),Swagger 2.9.2會(huì)有一些兼容性問題,很有可能在項(xiàng)目運(yùn)行前我們就飲恨于此了。

比較常見的幾個(gè)錯(cuò)誤:

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”。

簡(jiǎn)單的解決辦法: 

版本對(duì)應(yīng)(Maven官方倉(cāng)庫(kù):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

補(bǔ)充性說明

一、低版本過程性搭建

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 {
 
} // 只用這幾行代碼就可以運(yùn)行

二、高版本過程性搭建

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作為默認(rèn)的路徑匹配策略,而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配置類都是默認(rèn)情況,下面簡(jiǎn)單分享一下自定義配置項(xiàng)。

@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)文章請(qǐng)關(guān)注:http://blog.didispace.com/")
                .version("8.0")
                .build();
    }
}

以上就是SpringBoot任意版本集成Swagger各種版本的操作指南的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot集成Swagger的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論