项目情况:
最近开的项目是巴斯夫公司的BCG项目,项目的后台框架阶段我选择了Swagger框架来实现API的生成。
该项目的后台技术框架有:SpringBoot+Spring Security+JWT+MyBatis+Swagger+POI
Swagger介绍:
个人感觉Swagger相当的方便,通过注解的方式将Controller层的方法自动生成到swagger-ui.html中,在该页面可以看到输入参数的格式、访问路径、返回的响应参数等。与postman类似,但比postman方便一点。而且前后端分离的情况下,后台代码发布到服务器上就可以让前端开发查看到swagger-ui.html。这样每次后台修改过代码之后,代码一发布,前端就可以看到最新的接口信息,省去了写wiki之类的时间。
项目整合Swagger框架步骤:
第一步:添加相关依赖:
<!--swagger框架-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
第二步:建立配置类,添加配置信息(这里我添加的请求头Authorization是由于我使用了jwt框架首先登陆校验,需要swagger能够发送token信息)
package com.handlecar.basf_bcg_service.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.Document