一、什么是swagger
前后端分离开发模式中,api文档是最好的沟通方式。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
Swagger让前后端开发人员更有效沟通。
二、配置Swagger2
1、依赖
添加依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2、创建Swagger2配置文件
(1)创建配置类Swagger2Config
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2);
}
}
(2)然后重启项目,访问
三、常用功能
1、swagger对接口分类
(1)场景:未来我们在项目中会有很多很多的接口,那么我们最好是对接口进行分类。
(2)示例:比如把前台网站和后台管理系统的接口进行分类。
1)为【前台网站】接口分类:接着在上面的配置类Swagger2Config 中使用Docket自定义我们自己的配置:
① groupName("webApi") 自定义分组名
②apiInfo(webApiInfo()) 自定义api文档信息
③.paths(Predicates.and(PathSelectors.regex("/api/.*"))) 只显示api路径下的接口页面
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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 Swagger2Config {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//只显示api路径下的页面
.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
// 自定义api文档信息
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("一碗谦谦粉", "https://blog.csdn.net/weixin_45764765", "688@qq.com"))
.build();
}
}
2)同样的,我们再定义【后台管理系统】的接口分类
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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 Swagger2Config {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//只显示api路径下的页面
.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
@Bean
public Docket adminApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("adminApi")
.apiInfo(adminApiInfo())
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/admin/.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("一碗谦谦粉", "https://blog.csdn.net/weixin_45764765", "688@qq.com"))
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("后台管理系统-API文档")
.description("本文档描述了后台管理系统微服务接口定义")
.version("1.0")
.contact(new Contact("一碗谦谦粉", "https://blog.csdn.net/weixin_45764765", "688@qq.com"))
.build();
}
}
通过对url的约定,如后台管理系统的接口以/admin打头,前台网站的接口以/api打头,这样我们就会简单的获得两个分组,为接口分类,能够方便我们管理众多接口。
2、swagger类注解
swagger常用的类注解有:@Api
效果:
3、swagger方法注解
swagger常用的方法注解有:@ApiOperation
效果:指定接口的别名
4、swagger参数注解
swagger常用的方法注解有:@ApiParam(value = "讲师对象", required = true)
效果:value指定的参数的别名,required = true 必填
5、swagger字段注解
swagger常用的字段注解有:@ApiModelProperty
效果:example指定示例的数据格式,在时间上更常用