Swagger2 和 Swagger3 的区别与使用

Swagger2 和 Swagger3 的区别与使用

主要区别:

1 导入的依赖

注意:我springboot 使用的版本为4.3.1, 版本过高会报错

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>

Swagger3

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

2 启用的注解

Swagger2: @EnableSwagger2

Swagger3: @EnableOpenApi

3 页面的位置

Swagger2: http://localhost:8080/swagger-ui.html

Swagger3: http://localhost:8080/swagger-ui/

最后的斜杠不能省略

4 配置类区别

Swagger2

return new Docket(DocumentationType.SWAGGER_2)

Swagger3

return new Docket(DocumentationType.OAS_30)

后面附有配置类的全部代码

常用注解使用说明

v2 和v3 使用的文档注解都是相同的,这一点很棒

详细介绍地址:https://www.cnblogs.com/niudaben/p/11869869.html

配置类代码

Swagger2Config.java

package com.ljy.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //配置swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){

        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag) //swagger开关
                .groupName("狂神项目组")
                .select()
                //RequestHandlerSelectors,配置要扫描接口的方式
                //basePackage() 指定要扫描的包
                //any():扫描全部
                //none();不扫描
                //withClassAnnotation:扫描类上的注解
                //withMethodAnotation: 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.kuang.swagger.controller"))
                //paths() 过滤什么路径
                //.paths(PathSelectors.ant("/kuang/**"))
                .build();
    }

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("廖家银项目组");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("廖家银项目组2");
    }

    //配置Swagger信息 apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact =new Contact("廖家银","http://fjkldfdhfl.com","2254536570@qq.com");

        return new ApiInfo("廖家银的api文档 ",
                "非常可爱的描述",
                "1.0",
                "urn:tos",
                contact, "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

Swagger3Config.java

package com.ljy.swagger3.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

@Configuration
@EnableOpenApi
public class SwaggerConfig {
    //配置swagger的Docket的bean实例
    @Bean
    public Docket docket1(Environment environment){

        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .enable(flag) //swagger开关
                .groupName("狂神项目组")
                .select()
                //RequestHandlerSelectors,配置要扫描接口的方式
                //basePackage() 指定要扫描的包
                //any():扫描全部
                //none();不扫描
                //withClassAnnotation:扫描类上的注解
                //withMethodAnotation: 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.ljy.swagger3.controller"))
                //paths() 过滤什么路径
                //.paths(PathSelectors.ant("/kuang/**"))
                .build();
    }

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.OAS_30).groupName("家银项目组");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.OAS_30).groupName("廖家银项目组2");
    }

    //配置Swagger信息 apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact =new Contact("廖家银","http://fjkldfdhfl.com","2254536570@qq.com");

        return new ApiInfo("廖家银的api文档 ",
                "非常可爱的描述",
                "1.0",
                "urn:tos",
                contact, "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

​ 不需要设置swagger的选择启动环境功能的话,可以不用创建3个properties文件,并且删除配置类中的这些代码

        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);
        
                .enable(flag) //swagger开关

application.properties

spring.profiles.active=dev

还创建了application-dev.properties 和 application-pro.properties这两个配置文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值