springboot集成Swagger

本文介绍如何导入并配置Swagger的不同版本,包括2.x和3.x版本的Maven依赖配置,提供了详细的Swagger配置类示例,展示了如何通过配置实现接口文档的自动生成及不同环境下的启用策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入swagger坐标
2版本坐标:

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

3版本:

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

Swagger配置类

@Configuration
@EnableOpenApi //3版本
//@EnableSwagger2 //2版本
public class SwaggerConfig {
    //配置Swagger的docket的bean实例
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示的配置环境(application-dev.properties)
        Profiles of = Profiles.of("dev", "test");
        //判断项目环境是否存在
        boolean b = environment.acceptsProfiles(of);
        System.out.println(b);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启用swagger
                .enable(b)
                .groupName("111")
                .select()
                //RequestHandlerSelectors,配置要扫描接口的方式
                //basePackage 指定包路径
                //none 都不
                //any 全部
                //withClassAnnotation 类上的注解
                //withMethodAnnotation 方法上的注解
                .apis(RequestHandlerSelectors.basePackage("cn.it.controller"))
                //扫描路径 ant表达式匹配
                // .paths(PathSelectors.ant("/it/**"))
                .build()
                ;
    }
     //配置多个Docket 进行分组
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("all");
    }

    //配置Swagger的信息的apiInfo

    public ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("pgc", "https://blog.youkuaiyun.com/Simplg", "593631426@foxmail.com");

        return new ApiInfo("Pancras_gc", "若没有梦想,何必远方",
                "2.0", "http://www.baidu.com", contact,
                "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

3.0:http://localhost:8888/swagger-ui/index.html
2.0:http://localhost:8080/swagger-ui.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值