个人博客:兰陵笑笑生,欢迎浏览博客!html
上一章 SpringBoot入门实践(七)-Spring-Data-JPA实现数据访问当中,咱们介绍了如何在项目中使用 Spring Data JPA来进行数据库的访问。本章将继续探索springBoot的其余功能,如何快速高效的构建在线的API文档。java
现在先后端分离,那么在先后端对接的时候少不了接口的文档,过去咱们都是手写一个wold文档,我相信做为一个写代码的,最不肯意的就是写各种的文档。固然接口文档是必须的。可是咱们手动维护的API文档在效率方面的确比较慢。并且还容易出错,最最要命的是随着项目的迭代,接口在不断的改变,实时的更新接口文档,是一件很分神的事情,今天咱们就简单的介绍一个如何使用Swagger2快速的生成在线文档。web
在Swagger的官方网站上https://swagger.io/ 是这样据介绍的,Swagger是实现了OpenApi标准的文档使用工具,包含了各类开源的工具集。spring
在构建好Restful API 后,咱们首先在pom.xml文件中引入Swagger2和Swagger UI的依赖:数据库
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2Conf { /** * 控制http://localhost:8081/swagger-ui.html#/的显示 * @return */ @Bean public Docket controllerApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() .title("标题:某公司_接口文档") .description("描述:用于XXXX功能,具体包括XXX,XXX模块...") .contact(new Contact("Socks", null, null)) .version("版本号:1.0") .build()) .select() //选择的包名 .apis(RequestHandlerSelectors.basePackage("com")) .paths(PathSelectors.any()) .build(); } }
到这里,swagger2的应用能够说快速的入门,可使用了,可是咱们可能须要对Controller、方法作一些说明,咱们能够经过如下额API精细化管理能够实现。json
首先的介绍几个经常使用的注解:segmentfault
hidden:没什么用后端
@Api(value = "value", tags = "用戶管理", hidden = true) @RestController @RequestMapping("/api/v1/user") public class UserController { .... }
3.4.3 @ApiIgnore: 做用在REST API控制器方法上,则该API不会被显示出来:api
@ApiOperation(value = "删除列表", notes = "注意", response = HttpResponse.class) @ApiIgnore @DeleteMapping("/delete/{id}") public HttpResponse delete(@PathVariable Long id) { userMap.remove(id); return HttpResponse.ok(); }
@ApiImplicitParams是能够包含多个@ApiImplicitParam,如app
/** * 列表请求 * @return */ @ApiOperation(value = "用户列表",notes = "注意",response = HttpResponse.class) @RequestMapping(path = "/list",method = RequestMethod.PUT, consumes = "application/json") @ApiImplicitParams({ @ApiImplicitParam(name="userName",value = "用戶名称"), @ApiImplicitParam(name="age",value = "用戶年龄") }) public HttpResponse list(String userName ,Integer age) { List<User> user = new ArrayList<>(userMap.values()); return HttpResponse.ok().setData(user); }
这样API文档就丰富了不少:
在生成的在线API文档中,不少时候咱们须要对POJO中的字段作一些解释,好比下图的User添加一个描述,对每一个字段添加一个描述,以及他的类型作一个解释,这里固然也可以使用Swagger2的其余2个注解:@ApiModel和 @ApiModelProperty,没有使用注解的时候以下图:
package com.miroservice.chapter2.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(value = "user",description = "用户") public class User { @ApiModelProperty(value = "用户ID",notes = "notes",dataType = "long",required = false,allowEmptyValue = true) private Long id; @ApiModelProperty(value ="用户名称",notes = "notes",dataType = "String",required = false,allowEmptyValue = true) private String userName; @ApiModelProperty("用户年齡") private Long age; ... }
这样保存的用户的每一个属性都有了解释:
咱们使用Swagger2是为了方便开发,可是如在把这样的文档在生产环境开启,那我估计过不了多久工做就没了,如何才能在正式环境禁止使用Swagger2呢,其实很简单。咱们能够在配置swagger的使用,添加一个注解:
@ConditionalOnProperty,并经过配置文件配置swagger.enable=true的时候,才开启swagger,以下配置:
@Configuration @EnableSwagger2 @ConditionalOnProperty(name = "swagger.enable", havingValue = "true") public class Swagger2Conf { /** * 控制http://localhost:8081/swagger-ui.html#/的显示 * @return */ @Bean public Docket controllerApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() .title("标题:某公司_接口文档") .description("描述:用于XXXX功能,具体包括XXX,XXX模块...") .contact(new Contact("Socks", null, null)) .version("版本号:1.0") .build()) .select() //选择的包名 .apis(RequestHandlerSelectors.basePackage("com")) .paths(PathSelectors.any()) .build(); } }
这一篇文章简单的介绍了springBoot如何集成Swagger2快速的构建在线的API文档,包括各类注解的详细的解释,固然Swagger2的强大之处不只于此,还提供了更多扩展的功能,包括API分组,为每一个API配置Token,生成静态的文档等,有兴趣的能够本身深刻研究,本章的介绍对于平时的开发应该是够用了。
以上就是本期的分享,你还能够关注本博客的#Spring Boot入门实践系列!#
本文由博客一文多发平台 OpenWrite 发布!
个人博客[兰陵笑笑生] ( http://www.hao127.com.cn/),欢...!