背景
在前后端分离开发的时代,前端和后端的实时交互(后端接口改变前端可以及时看到),成为了一个主要问题之一,前后端沟通成本越来越高,前后端人员无法做到“即时协商”,最终导致问题爆发
以前的解决方案:
- 指定schema【计划提纲】,实时更新最新的API,降低集成的风险
- 制定word计划文档
- 前后端分离:
- 前端测试后端接口,工具:postman
- 后端提供接口,需要实时更新最新的消息及改动
简介
- 号称世界上最流行的API框架
- 是一套开源的框架
- RestFul Api文档在线自动生成工具 >>> Api文档与Api定义同步更新
- 直接运行,可以在线测试Api接口
- 支持多种语言
官网:https://swagger.io/
SpringBoot集成Swagger
需要的两个依赖:
新建一个SpringBoot-Web项目,并导入相关依赖
<dependencies>
<!-- 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>
编写一个Hello工程
/**
* @Author 凉白开_LBK
* @Date 2021/10/25 10:08
* @Explain
*/
@RestController
public class HelloController {
@RequestMapping("/HelloWorld")
public String hello(){
return "hello,world";
}
}
配置SwaggerConfig
测试运行:http://localhost:8080/swagger-ui.html
配置Swagger
package com.lbk.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi