[关闭]
@xiaoxiaowang 2017-05-03T16:01:32.000000Z 字数 5544 阅读 2362

如何优雅的“编写”api接口文档

api swagger springboot : IT


前言

  今天我需要把之前写的接口整理成文档,老大给了意见用swagger搞,我像发现新大陆一样的兴奋,迫不及待得去“占有”它。
  Swagger很容易上手,我花了十几分钟就搞定了。正好接着之前的如何优雅的格式化接口,这里再说一下SpringBoot整合Swagger的简单过程吧

Swagger介绍

  每每get新的技能想分享的时候,按照套路来讲,需要有一个版块将该技能的“前世今生”介绍个遍,但就我接触到完成配置不超过半小时,我觉得让我完完整整的介绍有点太虚了,所以,最好的介绍就是下面的官网
http://swagger.io/
http://swagger.io/irc/ 这个是实时聊天室,刚刚和老外沟通了一番“how are you?fine thk you.and you?”
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel 这个是一些注解的api
Swagger有三个模块

整合

  我接着之前的代码的写(可以在我的GitHub上浏览,或者直接clone到本地再切换到api-norms分支),这里要说一下,使用Springboot整合Swagger贼JB简单,相比较而言,SpringMVC就比较复杂了,这里暂且不谈(以后可能也不会谈了,自从我使用了Springboot之后,就已经开始抛弃SpringMVC了)

maven依赖

老规矩上配置

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>${swagger.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. <version>${swagger.version}</version>
  10. </dependency>

添加Swagger注解

  在Application上直接添加@EnableSwagger2,注意版本,官网上的版本还没有更新到最新的,最新的在Github上看,配置后的代码

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  4. /**
  5. * Created by wangxc on 2017/3/9.
  6. */
  7. @SpringBootApplication
  8. @EnableSwagger2
  9. public class Application {
  10. public static void main(String[] args) {
  11. SpringApplication.run(Application.class, args);
  12. }
  13. }

可以了,接下来就是描述接口的注解了!在Controller层,做如下配置

  1. package com.quick.api;
  2. import com.quick.po.Address;
  3. import com.quick.utils.BaseResp;
  4. import com.quick.utils.ResultStatus;
  5. import io.swagger.annotations.*;
  6. import org.springframework.util.StringUtils;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. /**
  14. * Created with IDEA
  15. * User: vector
  16. * Data: 2017/4/12
  17. * Time: 8:41
  18. * Description:
  19. */
  20. @RestController
  21. @RequestMapping("/api")
  22. @Api("springboot整合swagger2") // Swagger UI 对应api的标题描述
  23. public class WebController {
  24. @ApiOperation("获取地址信息") // 单个接口的描述
  25. @ApiImplicitParams({
  26. @ApiImplicitParam(paramType="query",name="province",dataType="String",required=true,value="省",defaultValue="广东省"),// 每个参数的类型,名称,数据类型,是否校验,描述,默认值(这些在界面上有展示)
  27. @ApiImplicitParam(paramType="query",name="area",dataType="String",required=true,value="地区",defaultValue="南山区"),
  28. @ApiImplicitParam(paramType="query",name="street",dataType="String",required=true,value="街道",defaultValue="桃园路"),
  29. @ApiImplicitParam(paramType="query",name="num",dataType="String",required=true,value="门牌号",defaultValue="666")
  30. })
  31. @ApiResponses({
  32. @ApiResponse(code=400,message="请求参数没填好"), // 响应对应编码的描述
  33. @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
  34. })
  35. @RequestMapping(value = "/address",method = RequestMethod.POST)
  36. public BaseResp<Address> getAddressInfo(@RequestParam(value = "province")String province,
  37. @RequestParam(value = "area")String area,
  38. @RequestParam(value = "street")String street,
  39. @RequestParam(value = "num")String num){
  40. if(StringUtils.isEmpty(province)||StringUtils.isEmpty(area)||StringUtils.isEmpty(street)||StringUtils.isEmpty(num)){
  41. return new BaseResp(ResultStatus.error_invalid_argument);
  42. }
  43. Address address = new Address();
  44. address.setProvince(province);
  45. address.setArea(area);
  46. address.setStreet(street);
  47. address.setNum(num);
  48. return new BaseResp(ResultStatus.SUCCESS,address);
  49. }
  50. @ApiOperation("获取地址信息")
  51. @ApiImplicitParams({
  52. @ApiImplicitParam(paramType="query",name="province",dataType="String",required=true,value="省",defaultValue="广东省"),
  53. @ApiImplicitParam(paramType="query",name="area",dataType="String",required=true,value="地区",defaultValue="南山区"),
  54. @ApiImplicitParam(paramType="query",name="street",dataType="String",required=true,value="街道",defaultValue="桃园路"),
  55. @ApiImplicitParam(paramType="query",name="num",dataType="String",required=true,value="门牌号",defaultValue="666")
  56. })
  57. @ApiResponses({
  58. @ApiResponse(code=400,message="请求参数没填好"),
  59. @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
  60. })
  61. @RequestMapping(value = "/address/list",method = RequestMethod.POST)
  62. public BaseResp<List<Address>> getAddressList(@RequestParam(value = "province")String province,
  63. @RequestParam(value = "area")String area,
  64. @RequestParam(value = "street")String street,
  65. @RequestParam(value = "num")String num){
  66. if(StringUtils.isEmpty(province)||StringUtils.isEmpty(area)||StringUtils.isEmpty(street)||StringUtils.isEmpty(num)){
  67. return new BaseResp(ResultStatus.error_invalid_argument);
  68. }
  69. Address address = new Address();
  70. address.setProvince(province);
  71. address.setArea(area);
  72. address.setStreet(street);
  73. address.setNum(num);
  74. List<Address> lists = new ArrayList<>();
  75. lists.add(address);
  76. lists.add(address);
  77. lists.add(address);
  78. return new BaseResp(ResultStatus.SUCCESS,lists);
  79. }
  80. }

我只是在原来的基础上添加了下面注解

名称 解释
@Api() 将类标记为一种Swagger资源。
@ApiOperation() 描述针对特定路径的操作或通常是 http 方法。
@ApiImplicitParams 允许多个 ApiImplicitParam 对象列表的包装。
@ApiImplicitParam 表示 api 操作中的单个参数。
@ApiResponses 允许多个 ApiResponse 对象列表的包装。
@ApiResponse 描述操作的可能响应。

更多的看这里

就这么简单,一个基本而又强大的API文档就整理好了!

启动

正常的启动SpringBoot,你会发现控制台输出了这些内容

  1. 2017-05-03 21:42:52,975 INFO ClassOrApiAnnotationResourceGrouping:100 - Group for method getAddressList was springboot整合swagger2
  2. 2017-05-03 21:42:52,986 INFO ClassOrApiAnnotationResourceGrouping:100 - Group for method getAddressList was springboot整合swagger2

说明Swagger已经成功跑起来了,接下来打开浏览器,输入你链接
yourdomain/swagger-ui.html
我的是http://localhost:8080/swagger-ui.html

相信你看了界面并四处点点之后,就会对上面注解的含义有了更进一步的了解~

后记

  这里展示的只是Swagger最基本的功能,更多强大的功能如果后面有运用,我会持续更新的。
  目前我在看api寻找SwaggerUI输入文件的测试,因为我有个接口需要上传文件,等我搞定,再来分享吧!!!
  

欢迎进入我的个人博客浏览

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注