一、依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
二、配置文件
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api1() {
//添加head参数start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.groupName("文档1名称接口")
.enable(true)
.apiInfo(apiInfo("接口1名称", "1.0"))
.select()
.apis(RequestHandlerSelectors.basePackage("com.zy"))
.paths(PathSelectors.regex("/path1.*"))//接口路径前缀
.build()
.globalOperationParameters(pars);
}
@Bean
public Docket api2() {
//添加head参数start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.groupName("文档2名称接口")
.enable(true)
.apiInfo(apiInfo("接口2名称", "1.0"))
.select()
.apis(RequestHandlerSelectors.basePackage("com.zy"))
.paths(PathSelectors.regex("/path2.*"))//接口路径前缀
.build()
.globalOperationParameters(pars);
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://ip:port/swagger-ui.html
*
* @return
*/
private ApiInfo apiInfo(String title, String version) {
return new ApiInfoBuilder()
.title(title)
.description("描述")
.termsOfServiceUrl("https://baidu.com")
.version(version)
.build();
}
}