认识界上最流行的Api框架——swagger
swagger简介
swagger是支持多种编程语言的Api框架。可以直接运行,在线测试API接口。有RestFul Api文档在线自动生成工具,并且能够达到Api文档与API定义同步更新。
由于前端和后端分离式开发的广泛应用,许多前端人员无法做到问题处理同步,为了提高问题的处理效率,以及避免工作中前后端工作人员的矛盾,就需要‘即时协商,目标同步’。对于这个问题,最早的解决方法是使用:指定schema并实时更新最新API、word计划文档、后端提供接口,前端用postman测试后端接口三种方法。但是这几种方法并不能达到即时的效果,所以swagger就应时而生。
作为世界上最流行的API框架,swagger在项目中使用时需要springfox(swagger2和swagger-ui)。这就需要在项目中导入以下两个依赖:
<!-- 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>
用SpringBoot集成swagger
-
新建SpringBoot web项目
-
导入swagger2和swagger-ui依赖
-
编写一个hello工程用于测试
-
配置swagger,编写config
@Configuration @EnableSwagger2 //开启Swagger2 public class SwaggerConfig { }
-
运行测试:http://localhost:8080/swagger-ui.html
配置swagger信息
-
swagger的bean实例docket:
@Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2); }
-
配置swagger的信息:
//配置Swagger信息=apiInfo private ApiInfo apiInfo(){ <