一:swagger是什么?
1、是一款让你更好的书写api文档规范且完整的框架。
2、提供描述、生产、消费和k可视化RESTful Web Service。
3、是由庞大工具集合支撑的形式化规范。这个集合涵盖了从终端用户接口、底层代码库到商业API管理的方方面面。
1. 前后端分离的特点
前后端分离是的前端与后端之间的职责更加明确
后台: 负责业务处理
前端: 负责显示逻辑
在这种情况下,前端和后端可以分别交付给专业的开发人员去做,所以是必须要定义前后端直接的对接 接口,否则各自为是则项目无法集成,这时就需要一个文档来定义统一的接口。
2. 在没有swagger之前
在没有swagger之间,我们可以使用word,excel等功能来书写接口定义文档,但又有一个弊端,即: 在接口发送改变时需要及时的同步接口文档,否则实际的接口与接口文档不相符,则接口文件就失去了 作用,甚至会起到反作用。
3. swagger的作用
根据在代码中使用自定义的注解来生成接口文档,这个在前后端分离的项目中很重要。这样做的好处是 在开发接口时可以通过swagger将接口文档定义好,同时也方便以后的维护。
4. swagger的优点
号称时最流行的API框架
接口文档在线生成,避免同步的麻烦
可以支持在线对接口执行测试
支持多语言
5. 集成swagger
打开https://mvnrepository.com/ ,查找springbox,在pom.xml中导入如下图标出的依赖
导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
编写sawgger的配置类
package com.zking.mini_program.config; 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.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; //跟着spring一起启动 @Configuration //打开swagger功能 @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(Documentation