在SpringBoot中搭建Swagger文档
1.导包
Maven依赖如下,版本自选(现在是2.9.2版本):
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
在项目中设置配置类
@Configuration
// 开启Swagger2的自动配置
@EnableSwagger2
public class SwaggerConfig {
}
配置Swagger实例
Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swaggger。
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
// 配置docket以配置Swagger具体参数
return new Docket(DocumentationType.SWAGGER_2);
}
}
配置好了启动项目
访问 http://localhost:8080/swagger-ui.html
配置API文档的信息
通过apiInfo()属性配置文档信息:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
private ApiInfo apiInfo(){
Contact contact =new Contact("联系人名字","联系人名字访问链接","联系人邮箱");
return new ApiInfo("Swagger学习文档", //标题
"这是学习swagger生成的文档", //描述
"v1.0", //版本
"http::localhost:8080", //"http://terms.service.url/组织链接", 组织链接
contact, // 联系人信息
"pach 2.0 许可", // 许可
"许可链接",