Spring Boot 整合Swagger 第二部 (动态配置启动Swagger,API分组管理)
Posted 是摩卡不是抹茶呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 整合Swagger 第二部 (动态配置启动Swagger,API分组管理)相关的知识,希望对你有一定的参考价值。
文章目录
动态配置启动Swagger
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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.Collections;
/**
* Swagger配置类
*
* @author MoCha
* @date 2019/5/24
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig
@Autowired
private Environment environment;
@Bean
public Docket apiDocket()
// 设置要显示swagger的环境
// 判断当前是处于该环境,通过 enable() 接收此参数判断是否要显示
boolean isEnable = environment.acceptsProfiles(Profiles.of("dev", "test"));
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("top.yangzefeng.swagger.controller"))
.paths(PathSelectors.any())
.build()
.enable(isEnable)
.apiInfo(apiInfo());
@Bean
public ApiInfo apiInfo()
return new ApiInfo(
"Spring Boot 整合 Swagger学习文档",
"最好的Swagger学习文档",
"V1.0",
"https://gitee.com/MoChaYZF",
new Contact("MoCha", "https://blog.csdn.net/Mr_Mocha", "123456@qq.com"),
"LICENSE",
"LICENSE URL",
Collections.emptyList());
打开生产环境下的配置,关闭Swagger
此时已经关闭Swagger
API分组管理
配置多个分组只需要配置多个docket即可
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.Collections;
/**
* Swagger配置类
*
* @author MoCha
* @date 2019/5/24
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig
@Bean
public Docket userApiDocket()
return new Docket(DocumentationType.SWAGGER_2)
.groupName("用户管理")
.select()
.apis(RequestHandlerSelectors.basePackage("top.yangzefeng.swagger.controller"))
.paths(PathSelectors.ant("/user/**"))
.build()
.apiInfo(userApiInfo());
@Bean
public ApiInfo userApiInfo()
return new ApiInfo(
"Spring Boot 整合 Swagger学习文档",
"最好的Swagger学习文档",
"V1.0",
"https://gitee.com/MoChaYZF",
new Contact("MoCha", "https://blog.csdn.net/Mr_Mocha", "123456@qq.com"),
"LICENSE",
"LICENSE URL",
Collections.emptyList());
@Bean
public Docket EmployeeApiDocket()
return new Docket(DocumentationType.SWAGGER_2)
.groupName("员工管理")
.select()
.apis(RequestHandlerSelectors.basePackage("top.yangzefeng.swagger.controller"))
.paths(PathSelectors.ant("/employee/**"))
.build()
.apiInfo(EmployeeApiInfo());
@Bean
public ApiInfo EmployeeApiInfo()
return new ApiInfo(
"Spring Boot 整合 Swagger学习文档",
"最好的Swagger学习文档",
"V1.0",
"https://gitee.com/MoChaYZF",
new Contact("MoCha", "https://blog.csdn.net/Mr_Mocha", "123456@qq.com"),
"LICENSE",
"LICENSE URL",
Collections.emptyList());
以上是关于Spring Boot 整合Swagger 第二部 (动态配置启动Swagger,API分组管理)的主要内容,如果未能解决你的问题,请参考以下文章