概述
前后端分离
Vue+SpringBoot
后端时代:前端只用管理静态页面;html==>后端。模板引擎JSP=>后端才是主力
前后端分离时代
- 前端 -> 前端控制层、视图层
- 伪造后端数据,json。已经存在了,不需要后端,前端工程队依旧能够跑起来
- 后端 -> 后端控制层、服务层、数据访问层
- 前后端通过API进行交互
- 前后端相对独立且松耦合
产生的问题
- 前后端集成联调,前端或者后端无法做到“及时协商,尽早解决”,最终导致问题集中爆发
解决方案
- 首先定义schema [ 计划的提纲 ],并实时跟踪最新的API,降低集成风险;
- 早些年:指定word计划文档;
- 前后端分离:
- 前端测试后端接口:postman
- 后端提供接口,需要实时更新最新的消息及改动
详细
- 号称世界上最流行的API框架
- Restful Api 文档在线自动生成器 => API 文档 与API 定义同步更新
- 直接运行,在线测试API
- 支持多种语言 (如:Java,PHP等)
- 官网:https://swagger.io/
快速开始
要求:jdk 1.8 + 否则swagger2无法运行
注意:
如果是报以下代码错误
Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
- 把springboot回退到
2.5.6
就能正常启动 - 或者在application.properties添加 spring.mvc.pathmatch.matching-strategy= ANT_PATH_MATCHER
原因:
这是因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。
步骤:
1. 新建一个SpringBoot-web项目
2. 添加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>
3. 编写HelloController,测试确保运行成功
4. 要使用Swagger,我们需要编写一个配置类-SwaggerConfig来配置 Swagger
@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {
}
5. 访问测试 :http