在项目开发过程中,总会牵扯到接口文档的设计与编写,之前使用的都是office工具,写一个文档,总也是不够漂亮和直观。好在git上的开源大神提供了生成文档的工具,so来介绍一下!

该工具是Nodejs的模块,请务必在使用前安装好nodejs环境!


工具名称:apiDoc

Git地址:https://github.com/apidoc/apidoc

项目地址:http://apidocjs.com/

样例项目:http://apidocjs.com/example_basic/

apoDoc是从源码的注释中生成RestFul api 文档,样子还是蛮漂亮的……

自己写了的ApiDoc文档自动生成工具,避免每次写完后手动执行生成代码

http://blog.csdn.net/soslinken/article/details/50476384

支持的注释样式:

JavaDoc-Style

  1. /**
  2. * This is a comment.
  3. */

CoffeeScript

  1. ###
  2. This is a comment.
  3. ###

Elixir

  1. @apidoc """
  2. This is a comment.
  3. """

Erlang(%不是必须的)

  1. %{
  2. % This is a comment.
  3. %}

Perl (Doxygen)

  1. #**
  2. # This is a comment.
  3. #*

Python

  1. """
  2. This is a comment.
  3. """

Ruby

  1. =begin
  2. This is a comment.
  3. =end

安装apiDoc

  1. npm install apidoc -g

使用npm命令安装apidoc即可!

使用方式:

在命令行中输入

  1. apidoc -f ".*\\.js$" -f ".*\\.java$" -i myapp/ -o apidoc/ -t mytemplate/

参数说明:

-f 文件过滤

使用正则表达式,表示哪些文件需要本转换,不设置的情况下,默认为.cs .dart .erl .go .java .js .php .py .rb .ts 后缀的文件。

-i 代码文件夹

-o 输出Api文档的路径

-t 使用模板文件的路径,可以自定义输出的模板

常用的命令格式如下:

  1. apidoc -i myapp/ -o apidoc/

配置

无package.json文件时,需要在代码文件夹的根目录下,创建apidoc.json文件。

  1. {
  2. "name": "example",
  3. "version": "0.1.0",
  4. "description": "apiDoc basic example",
  5. "title": "Custom apiDoc browser title",
  6. "url" : "https://api.github.com/v1"
  7. }

在该文件中即可配置apidoc

有package.json文件时,在package.json文件中添加”apidoc”: { }属性即可。

  1. {
  2. "name": "example",
  3. "version": "0.1.0",
  4. "description": "apiDoc basic example",
  5. "apidoc": {
  6. "title": "Custom apiDoc browser title",
  7. "url" : "https://api.github.com/v1"
  8. }
  9. }

配置属性如下:

name:项目名称

version:项目版本

description:项目介绍

title:浏览器显示的标题内容

url:endpoints的前缀,例如https://api.github.com/v1

sampleUrl:如果设置了,则在api文档中出现一个测试用的from表单

header

title:导航文字包含header.md文件

filename:markdown-file 文件名

footer

title:导航文字包含header.md文件

filename:markdown-file 文件名

order:用于配置输出 api-names/group-names 排序,在列表中的将按照列表中的顺序排序,不在列表中的名称将自动显示。


模板的配置:

在apidoc.json中或在package.json中添加template属性,将对模板进行特殊设置

forceLanguage:生成指定语言的文档,简体中文仅需设置”zh-cn”,支持的语言:https://github.com/apidoc/apidoc/tree/master/template/locales

withCompare:是否启用与旧版本的比较功能,默认为true

withGenerator:是否输出生成信息,默认为true

jQueryAjaxSetup:设置Ajax请求的默认值,参见http://api.jquery.com/jquery.ajaxsetup/


我使用的配置如下:

  1. {
  2. "name": "测试",
  3. "version": "0.0.1",
  4. "description": "API文档测试",
  5. "title": "API文档测试",
  6. "url" : "http://xxxxxxx",
  7. "sampleUrl" : "http://xxxxxxxx",
  8. "template":{
  9. "forceLanguage":"zh-cn"
  10. }
  11. }

先来个demo试试:

在myapp文件夹下创建example.java

  1. /**
  2. * @api {get} /user/:id Request User information
  3. * @apiName GetUser
  4. * @apiGroup User
  5. *
  6. * @apiParam {Number} id Users unique ID.
  7. *
  8. * @apiSuccess {String} firstname Firstname of the User.
  9. * @apiSuccess {String} lastname Lastname of the User.
  10. *
  11. * @apiSuccessExample Success-Response:
  12. * HTTP/1.1 200 OK
  13. * {
  14. * "firstname": "John",
  15. * "lastname": "Doe"
  16. * }
  17. *
  18. * @apiError UserNotFound The id of the User was not found.
  19. *
  20. * @apiErrorExample Error-Response:
  21. * HTTP/1.1 404 Not Found
  22. * {
  23. * "error": "UserNotFound"
  24. * }
  25. */

之后执行

  1. apidoc -i myapp/ -o apidoc/

打开apidoc文件夹下的index.html即可看到效果。赞赞哒……


重头戏来了,下面要讲解一下apiDoc的注释都是什么含义

@api

  1. @api {method} path [title]

使用该注释的才能被识别成为文档的一块内容

method:请求方法,参见https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

path:请求路径

title(可选):标题

Example:

  1. /**
  2. * @api {get} /user/:id
  3. */

@apiDefine

  1. @apiDefine name [title]
  2. [description]

定义重复内容,供其他模块引用

name:重复模块名

title(可选):标题

description(可选):说明

Example1:

  1. /**
  2. * @apiDefine MyError
  3. * @apiError UserNotFound The <code>id</code> of the User was not found.
  4. */
  5. /**
  6. * @api {get} /user/:id
  7. * @apiUse MyError
  8. */

Example2:

  1. /**
  2. * @apiDefine admin User access only
  3. * This optional description belong to to the group admin.
  4. */
  5. /**
  6. * @api {get} /user/:id
  7. * @apiPermission admin
  8. */

@apiDescription

  1. @apiDescription text

设置多行说明

text:多行说明内容

Example

  1. /**
  2. * @apiDescription This is the Description.
  3. * It is multiline capable.
  4. *
  5. * Last line of Description.
  6. */

@apiError

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

请求错误参数

(group)(可选):参数将以这个名称分组,不设置的话,默认是Error 4xx

{type}(可选):返回值类型,例如:{Boolean}, {Number}, {String}, {Object}, {String[]}

field:返回值字段名称

descriptionoptional(可选):返回值字段说明

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiError UserNotFound The <code>id</code> of the User was not found.
  4. */

@apiErrorExample

  1. @apiErrorExample [{type}] [title]
  2. example

错误返回信息样例。

type(可选):返回内容的格式

title(可选):标题

example:样例,可以是多行文本

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiErrorExample {json} Error-Response:
  4. * HTTP/1.1 404 Not Found
  5. * {
  6. * "error": "UserNotFound"
  7. * }
  8. */

@apiExample

  1. @apiExample [{type}] title
  2. example

请求Api的测试样例

{type}(可选):请求方式类型

title:标题

example:样例,可以是多行文本

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiExample {curl} Example usage:
  4. * curl -i http://localhost/user/4711
  5. */

@apiGroup

  1. @apiGroup name

定义Api的分组

name:组名称,也是导航的标题

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiGroup User
  4. */

@apiHeader

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

定义head参数

(group)(可选):分组

{type}(可选):类型,例如:{Boolean}, {Number}, {String}, {Object}, {String[]}

field:变量名

[field]:定义变量,并标记变量是可选项

=defaultValue(可选):默认值

description(optional):变量说明

Examples:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiHeader {String} access-key Users unique access-key.
  4. */

@apiHeaderExample

  1. @apiHeaderExample [{type}] [title]
  2. example

head参数样例

{type}(可选):请求方式类型

title:标题

example:样例,可以是多行文本

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiHeaderExample {json} Header-Example:
  4. * {
  5. * "Accept-Encoding": "Accept-Encoding: gzip, deflate"
  6. * }
  7. */

@apiIgnore

  1. @apiIgnore [hint]

忽略不发布到文档

hint(可选):不发布的原因

Example:

  1. /**
  2. * @apiIgnore Not finished Method
  3. * @api {get} /user/:id
  4. */

@apiName

  1. @apiName name

定义api名称

name:api名称

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiName GetUser
  4. */

@apiParam

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

定义请求Api的参数

(group)(可选):分组

{type}(可选):类型,例如:{Boolean}, {Number}, {String}, {Object}, {String[]}

{type{size}}(可选):类型限定长度,例如:{string{..5}} 限定最大长度为5个字符

{string{2..5}} 限定最短2个字符,最长5个字符

{number{100-999}} 限定数字在100-999之间

{type=allowedValues}(可选):类型限定值,例如:{string=”small”}限定只允许传递small字符,

{string=”small”,”huge”} 限定只允许传递small或huge字符,

{number=1,2,3,99} 限定只允许传1,2,3,99这四个数字

field:变量名

[field]:定义变量,并标记变量是可选项

=defaultValue(可选):默认值

description(optional):变量说明

Examples:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiParam {Number} id Users unique ID.
  4. */
  5. /**
  6. * @api {post} /user/
  7. * @apiParam {String} [firstname] Optional Firstname of the User.
  8. * @apiParam {String} lastname Mandatory Lastname.
  9. * @apiParam {String} country="DE" Mandatory with default value "DE".
  10. * @apiParam {Number} [age=18] Optional Age with default 18.
  11. *
  12. * @apiParam (Login) {String} pass Only logged in users can post this.
  13. * In generated documentation a separate
  14. * "Login" Block will be generated.
  15. */

@apiParamExample

  1. @apiParamExample [{type}] [title]
  2. example

请求参数样例

{type}(可选):请求方式类型

title:标题

example:样例,可以是多行文本

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiParamExample {json} Request-Example:
  4. * {
  5. * "id": 4711
  6. * }
  7. */

@apiPermission

  1. @apiPermission name

定义接口权限

name:权限名称

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiPermission none
  4. */

@apiSampleRequest

  1. @apiSampleRequest url
    • 1

设置测试请求form,如果在apidoc.json或package.json中设置过了sampleUrl,则默认请求地址为,sampleUrl+apipath,设置这个标签则重写测试请求路径

url:测试地址,

样例

@apiSampleRequest http://www.example.com 改变测试地址

@apiSampleRequest /my_test_path 在apipath中加前缀

@apiSampleRequest off 不显示测试请求from

Examples:

默认请求的地址是http://api.github.com/user/:id

  1. Configuration parameter sampleUrl: "http://api.github.com"
  2. /**
  3. * @api {get} /user/:id
  4. */

需要将测试地址修改为http://test.github.com/some_path/user/:id,则

  1. Configuration parameter sampleUrl: "http://api.github.com"
  2. /**
  3. * @api {get} /user/:id
  4. * @apiSampleRequest http://test.github.com/some_path/
  5. */

当地址需要请求http://api.github.com/test/user/:id

  1. Configuration parameter sampleUrl: "http://api.github.com"
  2. /**
  3. * @api {get} /user/:id
  4. * @apiSampleRequest /test
  5. */

不需要测试请求from

  1. Configuration parameter sampleUrl: "http://api.github.com"
  2. /**
  3. * @api {get} /user/:id
  4. * @apiSampleRequest off
  5. */

没有设置sampleUrl时需要显示测试请求from,并设置请求http://api.github.com/some_path/user/:id

  1. Configuration parameter sampleUrl is not set
  2. /**
  3. * @api {get} /user/:id
  4. * @apiSampleRequest http://api.github.com/some_path/
  5. */

@apiSuccess

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

请求成功返回的参数

(group)(可选):参数将以这个名称分组,不设置的话,默认是Error 4xx

{type}(可选):返回值类型,例如:{Boolean}, {Number}, {String}, {Object}, {String[]}

field:返回值字段名称

descriptionoptional(可选):返回值字段说明

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiSuccess (200) {String} firstname Firstname of the User.
  4. * @apiSuccess (200) {String} lastname Lastname of the User.
  5. */

@apiSuccessExample

  1. @apiSuccessExample [{type}] [title]
  2. example

请求成功返回数据样例

{type}(可选):请求方式类型

title:标题

example:样例,可以是多行文本

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiSuccessExample {json} Success-Response:
  4. * HTTP/1.1 200 OK
  5. * {
  6. * "firstname": "John",
  7. * "lastname": "Doe"
  8. * }
  9. */

@apiUse

  1. @apiUse name

使用在@apiDefine中定义的内容

name:在@apiDefine定义的name

Example:

  1. /**
  2. * @apiDefine MySuccess
  3. * @apiSuccess {string} firstname The users firstname.
  4. * @apiSuccess {number} age The users age.
  5. */
  6. /**
  7. * @api {get} /user/:id
  8. * @apiUse MySuccess
  9. */

@apiVersion

  1. @apiVersion version

设定Api的版本号

version:版本号,格式(major.minor.patch)

Example:

  1. /**
  2. * @api {get} /user/:id
  3. * @apiVersion 1.6.2
  4. */

以上就是apiDoc的介绍!谢谢各位看官……

附上一个样例

user.java

  1. /**
  2. * @api {POST} /register 注册用户
  3. * @apiGroup Users
  4. * @apiVersion 0.0.1
  5. * @apiDescription 用于注册用户
  6. * @apiParam {String} account 用户账户名
  7. * @apiParam {String} password 密码
  8. * @apiParam {String} mobile 手机号
  9. * @apiParam {int} vip = 0 是否注册Vip身份 0 普通用户 1 Vip用户
  10. * @apiParam {String} [recommend] 邀请码
  11. * @apiParamExample {json} 请求样例:
  12. * ?account=sodlinken&password=11223344&mobile=13739554137&vip=0&recommend=
  13. * @apiSuccess (200) {String} msg 信息
  14. * @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
  15. * @apiSuccessExample {json} 返回样例:
  16. * {"code":"0","msg":"注册成功"}
  17. */
  18. /**
  19. * @api {POST} /login 用户登录
  20. * @apiGroup Users
  21. * @apiVersion 0.0.1
  22. * @apiDescription 用于用户登录
  23. * @apiParam {String} userName 用户名
  24. * @apiParam {String} password 密码
  25. * @apiParamExample {json} 请求样例:
  26. * ?userName=张三&password=11223344
  27. * @apiSuccess (200) {String} msg 信息
  28. * @apiSuccess (200) {String} code 0 代表无错误 1代表有错误
  29. * @apiSuccess (200) {String} user 用户信息
  30. * @apiSuccess (200) {String} userId 用户id
  31. * @apiSuccessExample {json} 返回样例:
  32. * {"code":"0","msg":"登录成功","userId":"fe6386d550bd434b8cd994b58c3f8075"}
  33. */
  34. /**
  35. * @api {GET} /users/:id 获取用户信息
  36. * @apiGroup Users
  37. * @apiVersion 0.0.1
  38. * @apiDescription 获取用户信息
  39. * @apiSuccess (200) {String} msg 信息
  40. * @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
  41. * @apiSuccess (200) {String} name 真实姓名
  42. * @apiSuccess (200) {String} mobile 手机号
  43. * @apiSuccess (200) {String} birthday 生日
  44. * @apiSuccess (200) {String} email 邮箱
  45. * @apiSuccess (200) {String} summary 简介
  46. * @apiSuccess (200) {String} recommendCode 我的推荐码
  47. * @apiSuccess (200) {String} idCardNo 身份证号
  48. * @apiSuccess (200) {String} memberState 会员状态 0普通用户 1VIP 2账户冻结
  49. * @apiSuccess (200) {String} address 家庭住址
  50. * @apiSuccess (200) {String} money 账户现金
  51. * @apiSuccessExample {json} 返回样例:
  52. * {
  53. * "code": 0,
  54. * "msg": "",
  55. * "name": "真实姓名",
  56. * "mobile": 15808544477,
  57. * "birthday": "1990-03-05",
  58. * "email": "slocn@gamil.com",
  59. * "summary": "简介",
  60. * "recommendCode": "我的推荐码",
  61. * "idCardNo": "身份证号",
  62. * "memberState": 1,
  63. * "address": "家庭住址",
  64. * "money": "30.65"
  65. * }
  66. */
  67. /**
  68. * @api {POST} /users/:id 修改(完善)用户信息
  69. * @apiGroup Users
  70. * @apiVersion 0.0.1
  71. * @apiDescription 修改(完善)用户信息
  72. * @apiParam (200) {String} [name] 真实姓名
  73. * @apiParam (200) {String} [mobile] 手机号
  74. * @apiParam (200) {String} [birthday] 生日
  75. * @apiParam (200) {String} [email] 邮箱
  76. * @apiParam (200) {String} [summary] 简介
  77. * @apiParam (200) {String} [idCardNo] 身份证号
  78. * @apiParam (200) {String} [address] 家庭住址
  79. * @apiSuccess (200) {String} msg 信息
  80. * @apiSuccess (200) {int} code 0 代表无错误 1代表有错误
  81. * @apiSuccessExample {json} 返回样例:
  82. * {"code":"0","msg":"修改成功"}
  83. */

这个样例可以直接生成,生成后即可看到apidoc的效果

2018年1月22日 更新

看到有很多人说中文乱码的事情,今天特别更新说明一下

主要是 @apiGroup 不支持utf-8 字符串。仅支持ascii码。所以很多使用者要么只能用英文说明,要么就放弃这个工具。

在官方有个办法可以实现utf-8字符串放置在@apiGoup 中。

代码如下

  1. /**
  2. * @apiDefine userApiStr 用户接口文档
  3. */
  4. /**
  5. * @api {get} /user/:id Request User information
  6. * @apiName GetUser
  7. * @apiGroup userApiStr
  8. */

说明

1、@apiDefine 必须放置在 /* …../中,也必须与引用变量的地方分开。

2、@apiGroup 后 放置的是 @apiDefine 定义的 变量名

其实本质上是定义了一个引用变量,这个变量支持utf-8。

使用apidoc 生成Restful web Api文档的更多相关文章

  1. 使用apidoc 生成Restful web Api文档——新手问题与解决方法

    使用apidoc工具来给项目做接口文档,不仅有合理的源码注释,还可以生成对应的文档.是给源码写备注的一个极佳实践. 工具名称:apiDoc Git地址:https://github.com/apido ...

  2. apidoc 生成Restful web Api文档

    在服务器项目开发过程中,总会牵扯到接口文档的设计与编写,之前使用的都是office写一个文档,不够直观.下面介绍一种工具生成Apidoc.,该工具是Nodejs的模块,请务必在使用前安装好nodejs ...

  3. SpringBoot入门教程(二十)Swagger2-自动生成RESTful规范API文档

    Swagger2 方式,一定会让你有不一样的开发体验:功能丰富 :支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能:及时更新 :开发过程中花一点写注释的时间,就可以及时的更新API文档 ...

  4. 开发人员的福音:微软、谷歌、Mozilla将他们所有的web API文档放在同一个地方

    Tips 原文作者:Liam Tung  原文地址:Developers rejoice: Microsoft, Google, Mozilla are putting all their web A ...

  5. Node与apidoc的邂逅——NodeJS Restful 的API文档生成

    作为后台根据需求文档开发完成接口后,交付给前台(angular vue等)做开发,不可能让前台每个接口调用都去查看你的后台代码一点点查找.前台开发若不懂你的代码呢?让他一个接口一个接口去问你怎么调用, ...

  6. Swagger2 生成 Spring Boot API 文档

    Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.本文主要介绍了在 Spring Boot 添加 Swagger 支持, 生成可自动维护的 A ...

  7. Api文档生成工具与Api文档的传播(pdf)

    点击查看apidoc生成文档demo 1 环境和工具 win10 apidoc:注释生成api文档 wkhtmltopdf:apidoc生成的是html,不适合传播,于是通过wkhtmltopdf将h ...

  8. Web api 文档以及测试工具配置

    第一步: 创建web api 在nuget 上搜索 webapitestclient (包含预发行版) 然后在 /Areas/HelpPage/Views/Help/Api.cshtml 末尾 添加 ...

  9. web api 文档声明

    namespaceHelloWebAPI.Controllers{     usingHelloWebAPI.Models;     usingSystem;     usingSystem.Coll ...

随机推荐

  1. Project Euler 389 Platonic Dice (概率)

    题目链接: https://projecteuler.net/problem=389 题意: 掷一个正四面体骰子,记点数为\(T\). 掷\(T\)个正六面体骰子,记点数和为\(C\). 掷\(C\) ...

  2. Redis原理(二)

    运维 快照使用子进程是通过一个子进程完成, 它会比较的浪费资源的操作. 1.遍历整个内存,会增加系统负担. 2.io操作,降低redis性能. 一般都是主备,备用的进行持久化. Redis 4.0混合 ...

  3. ios系统和某些移动端background-attachment:fixed不兼容性

    固定背景不动:background-attachment:fixed; ios系统和某些移动端background-attachment:fixed不兼容性,没有任何效果,但可以hack一下就可以了, ...

  4. web前端背景介绍

    Internet:是一个全球性的计算机互联网络,中文名称“因特网”.“国际互联网”.“网际网”等等: Internet提供的服务:http.ftp.Telnet.email.www.bbs等等: 基本 ...

  5. [python]bug和debug

    bug:代码中存在的语法或者逻辑问题 debug:自查和解决代码中的问题 (coding五分钟,debug两小时) 一.出现bug原因的四大类型 1.粗心 1)错误案例 上面这个错误就是因为 if语句 ...

  6. 在设置了android:parentActivityName后,点击子Activity返回键,父Activity总会调用OnDestroy()的解决方式

    近期查了非常久这个事情.分享给大家, 原理非常easy,一个Activity在manifet里声明了android:parentActivityName:这时候通过Activity左上角的返回butt ...

  7. 我在世界最热创业孵化器YC学到的58件事

    Amir Elaguizy是网络扑克游戏平台MarketZero创始人,2011年,他创立的这家公司被Zynga收购,后在Zynga担任HTML5扑克游戏的CTO.目前他同时是社会化点评网站lark. ...

  8. Javascript和jquery事件--事件监听器

    之前看完了js和jq的冒泡捕获和事件对象event,这里看看同时使用js和jq后我最容易混淆的监听器的绑定. (1) js的监听器绑定解绑 绑定监听器有两种方式: a.on-事件type,比如oncl ...

  9. php课程 8-30 实现验证码验证的难点是什么

    php课程 8-30 实现验证码验证的难点是什么 一.总结 一句话总结:session技术实现验证码传递. 1.生成验证码的那个网页(php文件)中的验证码怎么搁到别的网页中去? 直接在img的src ...

  10. P2P系统哪家强,功能其实都一样

    现在的P2P平台有好几千家了,了解了其中的几十家,发现用户端的P2P界面功能都差不多.下面来做个简要的总结: 1.通用功能  注册.登录  2.投资理财  针对理财人的投标.债权转让  3.借款申请  ...