一、新建Api-blueprint文档1.1下载文档编辑工具Atom打开官网下载Atom1.2安装Atom插件安装Api-blueprint文档预览插件,可以实时查看language-api-blueprint:语法高亮api-blueprint-preview:实时预览安装方法:依次点击Atom->Preferences->Install,在搜索框中输入language-api-blueprint,选择Packages去搜索,在红框内的两个插件上点击Install,如下图就算安装完成了。插件下载安装好后,点击language-api-blueprint的设置,然后勾选:ShowIndentGuide:Showindentation,因为APIBluePrint对缩进的要求比较高,最好打开这个,就是方便故障排除;ShowInvisibles:显示空格、tab键和换行符;1.3新建文件打开Atom,Command+N新建一个tab,然后保存为api.apib,文件扩展名apib是APIBluePrint的缩写。语法高亮,实时预览效果如下:2.编写API文档官方文档官方示例我们主要会用到这几个部分:Metadata部分metadataAPIname&overview部分APInameanddescriptionResourcegroup部分ResourcegroupSchema部分Action部分action(POST,GET)RequestsectionRequestResponsesectionResponseURIparameterssectionRequestURLparametersHeaderssectionRequestheaderBodysectionRequestdata2.1MetadataMetadataExampleFORMAT:1A你需要用一个metadata(元数据)明确指定APIBlueprint的版本.例如,我们在上面的文件中使用的版本号是1A。2.2API名称和概述部分API名称和简要说明示例#基本ACME博客API欢迎使用**ACME博客**API。该API提供对**ACMEBlog**服务的访问。2.3资源组部分可以将接口划分为功能模块进行分类,Group后跟类名Example#GroupEmployeeManagement2.4Action部分这里定义POST/GET请求书写格式:#[]示例##商户列表查询[POST/store/app/search{?storeName,enable,pageNO,pageCount}]URI模板书写格式:以/store/app/search{?storeName,enable,pageNO,pageCount}为例。/store/app/search:URLlink{?storeName,enable,pageNO,pageCount}:大括号中的参数以?一开始,在参数之间划分2.5ResponsesectionResponse写法:+Response()Example+Response201(application/json){"message":"created"}2.6URIparameterssectionparametersExample+参数+storeName(string,required)-storename,string+enable(numberl,optional)-status,integer0:enable,1:logout-1defaultcheckall+pageNO(numberl,required)-页码,integerTypefirstpagedefault1required+pageCount(numberl,required)-每页显示的项目数,整数必填required/optional:如果不填,默认required2.7Headerssectionwritingformat::Example+HeadersAccept-Charset:utf-8Connection:keep-aliveContent-Type:multipart/form-data,boundary=AaB03x2.8BodysectionBody是文档的关键部分。里面的内容就是需要模拟的数据。即mockserver的数据源。Example1+Body{"message":"Hello"}Example2+Body{"success":true,"err_code":"888888","err_msg":"错误描述","data":{"id":39,"storeId":43,"realname":"屠神","mobilePhone":"13554477744","storeName":"杭州店","type":0,"sex":1,"enable":1,"username":"chenlong01","portrait":"Avatar"}}2.9Schema部分功能:返回数据说明注:以下Example对应2.8中的Example2。请参见示例+架构{“data”:{“id”:“员工ID,整数”,“storeId”:“商店ID,字符串”,“storeName”:“商店名称”,“realname”:“员工姓名,字符串”,"mobilePhone":"手机号码,字符串","sex":"性别,0:男,1:女","enable":"启用状态,0:启用;1:禁用","username":"账号,字符串","portrait":"头像链接,string"}}3.Demo创建了一个工程:ApiBlueprintDemo-masterProject包含:APIBueprint文档APIBueprint文档生成的HTML:可以在本地打开预览效果,支持在线查询参考文档使用API??BluePrint编写RESTful接口文档