apiDoc 使用指南

安装

  1. 安装node.js
  2. 安装apiDoc
npm install apidoc -g

配置

在你的项目根目录下新建apidoc.json文件,该文件描述了项目对外提供接口的概要信息如名称、版本、描述、文档打开时浏览器显示标题和接口缺省访问地址。
apidoc.json

{
  "name": "ServiceEbikeAPIs",
  "version": "3.1.0",
  "description": "车辆服务接口文档",
  "title": "ServiceEbikeAPIs",
  "url" : "http://cjl3.rokyinfo.net:7190/api-ebike"
}

使用样例

/**
 *
 * @apiDefine RkNotFoundException
 *
 * @apiError RkNotFoundException 找不到相关数据
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": {
 *           "code": 404,
 *           "msg": "",
 *           "path" ""
 *       }
 *     }
 *
 */

/**
 *
 * @api {get} /v3.1/ues/:sn/rt-info 获取设备上报实时信息
 * @apiVersion 3.1.0
 * @apiName GetUeRealTimeInfo
 * @apiGroup UE
 *
 * @apiHeader {String} Authorization 用户授权token
 * @apiHeader {String} firm 厂商编码
 * @apiHeaderExample {json} Header-Example:
 *     {
 *       "Authorization": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org",
 *       "firm": "cnE="
 *     }
 *
 * @apiParam {String} sn 设备序列号
 *
 * @apiSuccess {String} sn 设备序列号
 * @apiSuccess {Number} status 设备状态
 * @apiSuccess {Number} soc 电池电量百分比
 * @apiSuccess {Number} voltage 电池电压
 * @apiSuccess {Number} current 电池电流
 * @apiSuccess {Number} temperature 电池温度
 * @apiSuccess {String} reportTime 上报时间(yyyy-MM-dd HH:mm:ss)
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "sn": "P000000000",
 *       "status": 0,
 *       "soc": 80,
 *       "voltage": 60.0,
 *       "current": 10.0,
 *       "temperature": null,
 *       "reportTime": "2018-08-13 18:11:00"
 *     }
 *
 * @apiUse RkNotFoundException
 *
 */
@RequestMapping(value = "/{sn}/rt-info", method = RequestMethod.GET)
public UeRealTimeInfo getUeRealTimeInfo(@RequestHeader(Constants.HEADER_LOGIN_USER_KEY) long userId, @PathVariable("sn") String sn) {

    return ueService.getRealTimeInfo(sn);
}

@api

@api {method} path [title]

HTTP接口调用方法、路径及名称

@apiVersion

@apiVersion version

api 版本号

@apiName

@apiName name

api 名称

@apiGroup

@apiGroup name

api 分组

@apiHeader

@apiHeader [(group)] [{type}] [field=defaultValue] [description]

请求头参数

@apiParam

@apiParam [(group)] [{type}] [field=defaultValue] [description]

请求参数

@apiSuccess

@apiSuccess [(group)] [{type}] field [description]

返回数据描述

@apiSuccessExample

@apiSuccessExample [{type}] [title]
                   example

接口成功返回样例

@apiError

@apiError [(group)] [{type}] field [description]

接口失败描述

@apiErrorExample

@apiErrorExample [{type}] [title]
                 example

接口失败返回样例

@apiDefine

@apiDefine name [title]
                     [description]

类似于宏定义,可以被引用

@apiUse

@apiUse name

使用@apiDefine定义的描述

更详细的说明请参考官方文档http://apidocjs.com

生成文档

cd到apidoc.json所在路径(即项目根目录)执行如下命令即可

apidoc -i src/ -o apidoc/

执行成功后会生成apidoc文件夹如下图所示


图片

点开apidoc文件夹中index.html会发现已经生成的漂亮的api文档


图片
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,088评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,191评论 25 709
  • 去年有段时间得空,就把谷歌GAE的API权威指南看了一遍,收获颇丰,特别是在自己几乎独立开发了公司的云数据中心之后...
    骑单车的勋爵阅读 20,958评论 0 41
  • 今天和几位老哥哥小聚,其中一位孙哥明年退休,席间妙语如珠,颇让人深思。 孙哥说:事上最难的事是两件事:一件事是把你...
    老鹰飞阅读 2,656评论 0 0
  • 微风暖意,阳光明媚!今天天气很好,适合孩子们远足出行…… 由学校组织的远足外出活动在孩子们浩浩荡荡的激情中顺...
    忆甜人生阅读 1,643评论 5 1

友情链接更多精彩内容