一篇搞定SpringBoot任意版本集成Swagger各种版本

        引言:喜欢自学的新手们,在学习Swagger生成API文档的时候经常会遇到问题,而目前市面上大部分技术分享者的SpringBoot版本并没和我们的同步,导致一些一模一样的代码,在我们的项目上却无法使用,这是一个经常性的问题,本文章就旨在和大家列举几种常用的项目搭配,来解决访问网址: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”。

      简单的解决办法: 
  1. 版本对应(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)

  1. 直接改用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)最小环境搭建

*注意低版本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)最小环境搭建

重要:修改配置文件!修改配置文件!修改配置文件!重要的事说三遍!!!

# application.properties写法

# Spring Boot 2.6.X版本引入了新的路径匹配策略,这导致了与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高低环境搭建

       得多皮才非得高配低、低配高啊?我可没这闲工夫再写了,上面两种解决方法自己排列组合试去吧

四、配置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构建RESTful APIs")
                .description("我可是描述信息哈~~更多Spring Boot相关文章请关注:http://blog.didispace.com/")
                .version("8.0")
                .build();
    }
}

​​​​​​​

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ℋᙚᵐⁱᒻᵉ鲸落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值