现代化的研发组织架构中,一个研发团队基本包括了产品组、后端组、前端组、APP端研发、 测试组、 UI组等,各个细分组织人员各司其职,共同完成产品的全周期工做。如何进行组织架构内的有效高效沟通就显得尤为重要。其中,如何 构建一份合理高效的接口文档更显重要。
<!-- 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>
@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
@RestController @RequestMapping("/video") @Api(value = "视频相关业务的接口",tags = {"视频相关业务的controller"}) public class VideoController { }
@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 }
@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,那么文档里面上传的参数就不会显示出来javascript