1、简介
后端时代:
- 前端只用管理静态页面 ==> 后端:模板引擎,如Jsp=> 后端是主力
前后端分离时代
- 后端 MVC
- 控制层
- 服务层
- 数据访问层
- 前端 MVVM
- 前端控制层
- 视图层
- Vue + SpringBoot ==> 主流技术栈
- 前后端如何交互 ==> API
- 前后端相对独立,松耦合;可以部署在不同的服务器上;
交互问题
- 前后端集成联调的时候无法做到及时协商。尽早解决,最终导致问题集中爆发
解决方案
- 指定Schema[计划提纲],实时更新最新的API,降低集成风险;
- 早些年指定Word计划文档
- 前后端分离
- 前端测试后端的接口——postman
- 后端提供接口,需要实时更新最新的消息及改动!
Swagger
号称世界上最流行的Api框架啊
RestFul 风格的 Api 文档在线自动生成工具 ==> 制作API 文档 与 API 定义同步更新
可直接运行,可以在线测试API接口
支持多种语言:JAVA 、PHP
官网:https://swagger.io/
在项目中使用Swagger 需要 Springfox
- Swagger2
- UI
2、SpringBoot集成Swagger
导包
<!-- 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>
3、配置Swagger
Config
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}
4、测试运行
http://localhost:8080/swagger-ui.html
5、配置Swagger
Swagger的bean实例Docket
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//配置 Swagger的Docket的Bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置Swagger 信息apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact DEFAULT_CONTACT = new Contact("时倾", "http://www.baidu.com/", "1327017819@qq.com");
return new ApiInfo("时倾的SwaggerAPI文档",
"迎风起势!",
"1.0",
"http://www.baidu.com/",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
6、配置扫描接口及开关
Docket ——select()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CrtYjS8A-1603600106062)(C:\Users\acer\AppData\Roaming\Typora\typora-user-images\image-20201023095340317.png)]
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(false)//设置是否启动Swagger
.select()
//RequestHandlerSelectors,配置要扫描的接口方法
//basePackage:指定要扫描的包
//any():扫描全部
//none():都不扫描
//withClassAnnotation():扫描类上的注解——参数是一个注解的反射对象
//withMethodAnnotation():扫描方法上的注解——get post
.apis(RequestHandlerSelectors.basePackage("com.lsw.swagger.controller"))
.paths(PathSelectors.ant("/lsw/**"))//过滤地址
.build();//工厂模式
}
只希望Swagger在生产环境下使用,在发布环境下不使用
- 判断是不是生产环境 flag = false
- 注入enable()
spring.profiles.active=pro
//设置要显示Swagger的环境
Profiles profiles = Profiles.of("dev");
//获取项目环境
//通过 environment.acceptsProfiles 判断是否处在自己设定的环境的当中
boolean flag = environment.acceptsProfiles(profiles);
if (flag){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(
RequestHandlerSelectors.basePackage("com.lsw.swagger.controller"))
.build();//工厂模式
}
else {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(false)//设置是否启动Swagger
.select()
.apis(RequestHandlerSelectors.basePackage("com.lsw.swagger.controller"))
.build();//工厂模式
}
7、配置API分组
.groupName("时倾")
配置多个Docket分组
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("B");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("C");
}
8、实体类配置
//只有我们的接口中返回值中存在实体类就会被扫描到Swagger中
@PostMapping(value = "/user")
public User user(){
return new User();
}
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
9、小结
- 我们可以通过Swagger给一些比较难理解的属性或者接口,添加注释信息
- 接口文档实时更新
- 可以进行API的 在线测试
正式发布的时候关闭Swagger,出于安全考虑;也节省内存