1

一、为什么要使用Swagger2

现代化的研发组织架构中,一个研发团队基本包括了产品组、后端组、前端组、APP端研发、 测试组、 UI组等,各个细分组织人员各司其职,共同完成产品的全周期工作。如何进行组织架构内的有效高效沟通就显得尤其重要。其中,如何构建一份合理高效的接口文档更显重要。

二、常用的注解

clipboard.png

三、使用步骤

1、导入依赖

<!-- swagger2 配置 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

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", "scau_zns@163.com"))
                // 描述
                .description("欢迎访问短视频接口文档,这里是描述信息")
                // 定义版本号
                .version("1.0").build();
    }
}
访问http://localhost:8080/swagger-ui.html

clipboard.png

3、配置某个Controller

@RestController
@RequestMapping("/video")
@Api(value = "视频相关业务的接口",tags = {"视频相关业务的controller"})
public class VideoController {
}

clipboard.png

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 {

}                                                                   

clipboard.png

注意到消息头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,那么文档里面上传的参数就不会显示出来
clipboard.png

6、测试接口

clipboard.png


nathan
57 声望14 粉丝

时刻保持积极向上的心态和清醒的大脑!


引用和评论

0 条评论