编写和维护接口文档是每个程序员的职责,根据Swagger2可以快速帮助我们编写最新的API接口文档,再也不用担心开会前仍忙于整理各种资料了,间接提升了团队开发的沟通效率。
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
2.9.2 io.springfox springfox-swagger2 ${swagger.version} io.springfox springfox-swagger-ui ${swagger.version}
@Configuration @EnableSwagger2 public class SwaggerConfiguration { @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); } private ApiInfo apiInfo(){ Contact ct = new Contact("cm", "http://cm.sina.cn", "cm@sina.com"); return new ApiInfo("我的塔吊信息测试","塔吊基础信息测试....","v1.0","http://www.bdqn.cn",ct,"学术部 2.0许可","许可连接",new ArrayList()); } }
@Api() @RestController @RequestMapping("/tcinfo") public class TcinfosCtrl {
/** * 6 按照用户传入的塔机编号 查询塔机信息 * @param tcid * @return */ @GetMapping("/findTcById") @ApiOperation("按照用户传入的塔机编号 查询塔机信息") @ApiImplicitParams({ @ApiImplicitParam(name = "tcid",value = "吊塔编号",required = true,paramType = "query",dataType = "int") }) public TcinfosVO findById(@RequestParam(value = "tcid")Integer tcid){ Tcinfos tic = tcinfoStoreService.findTcByTcid(tcid); TcinfosVO tvo = new TcinfosVO(); BeanUtils.copyProperties(tic,tvo); return tvo; }}
4 启动 浏览器输入地址 开始接口测试
http://localhost:9090/swagger-ui.html#/
//9090 用自己的端口