一、为什么要使用Swagger2
现代化的研发组织架构中,一个研发团队基本包括了产品组、后端组、前端组、APP端研发、 测试组、 UI组等,各个细分组织人员各司其职,共同完成产品的全周期工作。如何进行组织架构内的有效高效沟通就显得尤其重要。其中,如何 构建一份合理高效的接口文档更显重要。
二、常用的注解
三、使用步骤
1、导入依赖
io.springfox
springfox-swagger2
2.4.0
io.springfox
springfox-swagger-ui
2.4.0
2、编写Swagger2的配置类
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* @Description:swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.imooc.controller"))
.paths(PathSelectors.any()).build();
}
/**
* @Description: 构建 api文档的信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("使用swagger2构建短视频后端api接口文档")
// 设置联系人
.contact(new Contact("imooc-Nathan", "http://www.imooc.com", "[email protected]"))
// 描述
.description("欢迎访问短视频接口文档,这里是描述信息")
// 定义版本号
.version("1.0").build();
}
}
访问 http://localhost:8080/swagger-ui.html
3、配置某个Controller
@RestController
@RequestMapping("/video")
@Api(value = "视频相关业务的接口",tags = {"视频相关业务的controller"})
public class VideoController {
}
4、配置某个接口方法
@ApiOperation(value = "上传视频",notes = "上传视频的接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "bgmId", value = "背景音乐id", required = false, dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "videoSeconds", value = "背景音乐的播放长度", required = true, dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "videoWidth", value = "视频宽度", required = true, dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "videoHeight", value = "视频高度", required = true, dataType = "String", paramType = "form"),
@ApiImplicitParam(name = "desc", value = "视频描述", required = false, dataType = "String", paramType = "form")
})
@PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
public IMoocJSONResult upload(String userId, String bgmId, double videoSeconds, int videoWidth, int videoHeight, String desc,
@ApiParam(value = "短视频", required = true) MultipartFile file) throws Exception {
}
注意到消息头headers = "content-type=multipart/form-data",那么前端传过来的参数
formData:{
userId: userInfo.id,
bgmId: bgmId,
desc: desc,
videoSeconds: duration,
videoWidth: tmpWidth,
videoHeight: tmpHeight
}
5、用对象来接收参数可以在pojo上做配置
@ApiModel(value = "用户对象",description = "这是用户对象 ")
public class Users {
@ApiModelProperty(hidden = true)
private String id;
@ApiModelProperty(value = "用户名", name = "username",example = "imoocuser",required = true)
private String username;
@ApiModelProperty(value = "密码", name = "password",example = "123456",required = true)
private String password;
@ApiModelProperty(hidden = true)
private String faceImage;
private String nickname;
@ApiModelProperty(hidden = true)
private Integer fansCounts;
@ApiModelProperty(hidden = true)
private Integer followCounts;
@ApiModelProperty(hidden = true)
private Integer receiveLikeCounts;
}
如果设置了hidden = true,那么文档里面上传的参数就不会显示出来
6、测试接口