我们 扫描的 接口(Controller)肯定 不能 是 让它 默认扫描呀。我们肯定要 自定义扫描呀,就是要指定 扫描的位置呀。要不然 它 的 自由度 岂不是太小了。
Docket.select().apis():选择 接口,得提供一个 请求处理的选择器。
Docket.select().apis(RequestHandlerSelectors.basePackage("直接提供包名的路径")):通过 提供一个 包的 路径 然后 选择 扫描的包。
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("top.muquanyu.controller"))
.build();
}

RequestHandlerSelectors 其实是有很多的方法的。
any():扫描所有的 Controllernone():不扫描任何东西withMethodAnnotation():如果 方法上有 注解,就扫描。withClassAnnotation():如果 Controller 类上 有注解,就扫描。那么问题来了,这个所谓的 注解 是啥呢?
答:可以是 你提供的 任何 一个注解。它的意思是 只要 这个方法上有 你提供的这个注解 我们就扫描!
@Api 注解@请求方式 注解


Docket.enable(false):关闭 Swagger
Docket.enable(true):开启 Swagger
就是 我们可能 要求 只扫描 一种特性 url 请求。也就是 这个接口 的 url 必须 是 符合我们 要求的。我们 才会 扫描。
Docket.paths(PathSelectors.ant("/mqy/**")):url 请求 必须是 /mqy/** 这种的,才可以。


比如说 我只希望 我的 Swagger 在 生产环境中 使用,在 发布的时候 不使用。
这个其实 不要太简单,因为 我么 直接 走 application 配置文件 就可以实现了。
spring:
profiles:
active: dev
mvc:
pathmatch:
matching-strategy: ant_path_matcher

@Bean
public Docket docket(Environment environment){
// 设置 需要 使用 Swagger 的环境,就是指定的 Profile
Profiles profiles = Profiles.of("dev","test"); // 这两个是 通过的环境
boolean flag = environment.acceptsProfiles(profiles); // 是否是 通过的 那个 环境
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("top.muquanyu.controller"))
.paths(PathSelectors.ant("/test/**"))
.build();
}
Environment environment:这个东西 SpringBoot 提供 使用的,可以 通过它 对 环境 做一些 操作。
environment.acceptsProfiles(profiles ):意思是 我们 通过 提供的 profiles 对象,然后 判断 当前的环境 是否 是 可以 通过的 环境。
Profiles profiles = Profiles.of("dev","test"); 设置两个通过 的 环境,其实 可以设置 n 个。
