Swagger使用Docket中使用groupName()实现分组
1、简介
- 只需要在Docket中设置groupName(),即可完成分组
@Bean
public Docket docket01() {
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
2、SwaggerConfig中的配置
package com.sgz.swagger.config;
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.ApiInfoBuilder;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(Environment environment) {
Profiles profiles = Profiles.of("dev", "test");
boolean flag = environment.acceptsProfiles(profiles);
System.out.println(flag);
ApiInfo apiInfo = new ApiInfoBuilder()
.contact(new Contact("我", "https://www.baidu.com", "xxxxxx@qq.com"))
.title("我的Swagger API开发文档")
.description("我的文档描述")
.version("1.1")
.build();
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.enable(flag)
.groupName("我")
.select()
.apis(RequestHandlerSelectors.basePackage("com.sgz.swagger.controller"))
.paths(PathSelectors.regex("/swagger/.*"))
.build();
}
@Bean
public Docket docket01() {
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket02() {
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket03() {
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
}
3、运行效果