【Swagger2源码级教学】04Swagger2的开启与关闭

enable() 方法 控制Swagger2 的开启与关闭

在这里插入图片描述

代码如下:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
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;

/**
 * TODO
 *
 * @author ben
 * @version 1.0
 * @date 2022/9/13 11:23
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                //配置文档信息
                .apiInfo(apiInfo())
                //控制Swagger2的开启与关闭
                .enable(true)
                //扫描行为
                .select()
                //接口扫描 不匹配任何controller的接口
                // .apis(RequestHandlerSelectors.none())
                //扫描指定包路径  参数为 包路径的字符串
                .apis(RequestHandlerSelectors.basePackage("cn.ben.for_learn_projects.swagger2config.controller"))
                /// 任何请求都扫描
                .paths(PathSelectors.any())
                .build();
    }

    //配置文档信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("ben", "https://xxxxxx.com", "Swagger2Config@SpringBoot.com");
        return new ApiInfo(
                "Ben的学习总结", // 标题
                "Swagger2Config", // 描述
                "v1.0", // 版本
                "https://xxxxxx.com", // 组织链接
                contact, // 联系人信息
                "Apach 2.0 许可", // 许可
                "https://xxxxxx.com", // 许可连接
                new ArrayList<>()// 扩展
        );
    }
}

通常,对于Swagger的配置信息,会使用一个对象(SwaggerProperties)集中配置信息,然后在SwaggerConfig中使用该对象进行配置,而对于enable()方法的参数,一般在开发环境为true,生产环境false,这个时候就会从yml中读取,然后在放入SwaggerConfig中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值