Springboot集成Swagger2和Swagger3

Springboot集成Swagger2和Swagger3

github仓库地址:https://github.com/xiaoxingOvO/springboot-study

前言

Swagger3是在Swagger2上做了大版本升级,使用方式差别不大,需要注意的是部分区别和配置的不同(如下表所示)。下面分别介绍了Springboot整合Swagger2和Swagger3的过程。

区别Swagger2Swagger3
依赖springfox-swagger2、springfox-swagger-uispringfox-boot-starter
启动注解@EnableSwagger2@EnableOpenApi
Document类型DocumentationType.SWAGGER_2DocumentationType.OAS_30
访问地址http://ip:port/context-path/swagger-ui.htmlhttp://ip:port/context-path/swagger-ui/index.html

访问地址记得拼接服务名,如果没有在配置文件中配置服务名,可以忽略。

1、pom文件中引入Swagger依赖

Swagger2

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

这里使用的Swagger版本是2.9.2、Springboot 版本是2.5.14,如果使用的Springboot版本过高,启动会报错。Springboot版本在2.6以下即可正常启动。

Swagger3

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

这里使用的Springboot版本是2.7.6,启动正常。

Springboot版本到2.6.0以上,启动报错 Failed to start bean ‘documentationPluginsBootstrapper‘ 问题处理

原因:Spring Boot 2.6.0开始使用基于PathPatternParser的路径匹配,而Springfox版本一直没有更新还是使用的AntPathMatcher导致了这个问题。

解决:修改yaml文件,将SpringBoot路劲匹配模式修改为AntPathMatcher就可以了,配置如下:

spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER

使用swagger-bootstrap-ui

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>

引入依赖后启动项目访问地址变成:http://ip:port/content-path/doc.html

2、swagger配置

Swagger2

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("1.0")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger2 RestFul APIs")
                .description("swagger2接口文档")
                .contact(new Contact("xx", "http://blog.csdn.net", "xx@email.com"))
                .version("1.0")
                .build();
    }
}

Swagger3

@Configuration
@EnableOpenApi
public class Swagger3Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .groupName("1.0")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger3 RestFul APIs")
                .description("swagger3接口文档")
                .contact(new Contact("xx", "http://blog.csdn.net", "xx@email.com"))
                .version("1.0")
                .build();
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值