现在application.yaml中分离环境
spring:
profiles:
active: dev
此时运行的时候生产环境
swagger中我们有两种方式,一种是直接用@Value注解的方法,简单粗暴
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//直接通过value获取到配置文件的值
//对应的application-dev.properties文件中会写上swagger.enable=boolean类型的值
@Value("${swagger.enable}")
private boolean enable;
//配置swagger的bean实例
@Bean
public Docket docket(Environment environment){
return new Docket(DocumentationType.SWAGGER_2)
//配置swagger信息
.apiInfo(apiInfo())
.enable(enable)
.select()
.apis(RequestHandlerSelectors.basePackage("com.kuang.controller"))
//paths过滤路径
//ant设置过滤的路径
//.paths(PathSelectors.ant("kuang/**"))
.build();
}
//配置swagger信息的类,通过DocumentationType.SWAGGER_2.apiInfo调用这个方法
//这些配置的大部分是没有什么用的,都是用来显示的,但是就算没用我们也要赔,因为我们点进去ApiInfo这个方法
//我们就会发现它没有set方法,只有get方法,所以就算没用只是用来显示,我们也要将其配置好,就算是乱输入,也要输入
//不然你们懂的
public ApiInfo apiInfo(){
Contact contact = new Contact("蒋锦华", "https://i.csdn.net/#/uc/profile", "1661259340@qq.com");
return new ApiInfo("蒋锦华的swagger日志",
"你尽管努力,其他的交给天意",
"1.0",
"https://i.csdn.net/#/uc/profile",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
我们要在我们的application-dev.properties文件中找到@Value("${swagger.enable}")中的swagger.enable配置类
true为开启false为关闭
还有一种是Profiles的方式
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//配置swagger的bean实例
@Bean
public Docket docket(Environment environment){
//Profiles直接获取环
Profiles profiles = Profiles.of("dev","test");
//获取环境,点进源码,查看方法以及参数值
//返回值为boolean acceptsProfiles(Profiles var1);boolea类型
boolean enable = environment.acceptsProfiles(profiles);
//Docket的参数点进Docket源码中,我们重写的是这个方法,所以我们也要传入这个参数
//DocumentationType.SWAGGER_2参数的很多的值可以配置
return new Docket(DocumentationType.SWAGGER_2)
//配置swagger信息
.apiInfo(apiInfo())
.enable(enable)
.select()
//RequestHandlerSelectors扫描方式
//basePackage扫描包
//any全部扫描
//none全部不扫描
//withClassAnnotation扫描class的注解 ,参数是一个注解的反射对象 如 RestController.class
//withMethodAnnotation扫描方法上的注解 如 GetMapper.class
.apis(RequestHandlerSelectors.basePackage("com.kuang.controller"))
//paths过滤路径
//ant设置过滤的路径
//.paths(PathSelectors.ant("kuang/**"))
.build();
}
//配置swagger信息的类,通过DocumentationType.SWAGGER_2.apiInfo调用这个方法
//这些配置的大部分是没有什么用的,都是用来显示的,但是就算没用我们也要赔,因为我们点进去ApiInfo这个方法
//我们就会发现它没有set方法,只有get方法,所以就算没用只是用来显示,我们也要将其配置好,就算是乱输入,也要输入
//不然你们懂的
public ApiInfo apiInfo(){
Contact contact = new Contact("蒋锦华", "https://i.csdn.net/#/uc/profile", "1661259340@qq.com");
return new ApiInfo("蒋锦华的swagger日志",
"你尽管努力,其他的交给天意",
"1.0",
"https://i.csdn.net/#/uc/profile",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
具体原理里面有注解,我写代码会把我理解到的都写进注解里
把博客当笔记记录一下我们的demo大部分就是这样的,如果没听懂的我推荐大家去B站看狂神的讲解的swagger,通俗易懂