package com.oo.swagger_demo.config;
/**
* Description:
*
* @author:wuxinke
* @date: 2021-06-07 23:02
* @param:
* @return:
*/
import org.springframework.context.annotation.*;
import org.springframework.core.env.*;
import springfox.documentation.builders.*;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
@Bean
//Environment:获取当前的生产环境
public Docket createRestApi(Environment environment){
//设置要显示的swagger环境
Profiles profiles = Profiles.of("dev");
//通过environment.acceptsProfiles(),判断是否处在自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//.enable(flag)//是否启动swagger,如果为false,则swagger不能再游览器中访问
.select()
//RequestHandlerSelectors.any():扫描全部
//RequestHandlerSelectors.none():不扫描
//RequestHandlerSelectors.basePackage():扫描指定包
//RequestHandlerSelectors.withClassAnnotation():扫描类上的注解,参数是一个注解的发射对象
//RequestHandlerSelectors.withMethodAnnotation():扫描方法上的注解,参数是一个注解的发射对象
.apis(RequestHandlerSelectors.basePackage("com.oo.swagger_demo.controller"))//扫描接口
//.paths(PathSelectors.ant("/admin/**"))//过滤什么路径
.build();
}
private ApiInfo apiInfo(){
Contact contact = new Contact("", "", "");
return new ApiInfo("吴新科接口", "属于吴新科第一次开发swagger接口测试", "1.0", "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}