一种web接口命名定义

开头直接给网站,阮一峰老师介绍东西就是到位:http://www.ruanyifeng.com/blog/2011/09/restful.html

当然了规范归规范,但是大家在实际工作中,为了方便自己项目的开发,会结合自己情况,对规范做出调整形成自己的接口模式,

毕竟没有硬性要求。

下面介绍一下自己一套定义方式: 

①:定义两种请求类型:get(查询),post(增,删,改)

②:路径命名规范: 全部小写,禁止大写和下划线

         错误示范:

          http:://127.0.0.1/api/v1/content/wordList?id=1

          http:://127.0.0.1/api/v1/content/word_list?id=1

③:显式的标志出是什么请求(毕竟比让人看get请求这种隐式的规范才知道是查询来的实在)

        显式标识动词:

        新增:add        

        更新:update

        删除:delete

        查询:get

        http:://127.0.0.1/api/v1/content/word/getlist?wordId=1   或者   http://127.0.0.1/api/v1/content/getwordlist?wordId=1

        http:://127.0.0.1/api/v1/content/word/add   或者   http://127.0.0.1/api/v1/content/addword

        http:://127.0.0.1/api/v1/content/word/update或者   http://127.0.0.1/api/v1/content/updateword

        http:://127.0.0.1/api/v1/content/word/delete或者   http://127.0.0.1/api/v1/content/deleteword

        如果是批量修改:

        http:://127.0.0.1/api/v1/content/words/update或者   http://127.0.0.1/api/v1/content/updatewords

④:禁止使用无意义的单词,例如:index,event,home.类似这种

        http:://127.0.0.1/api/v1/content/index

        http:://127.0.0.1/api/v1/content/xxxxx

⑤:版本化

        加个v1,v2,什么的,这个东西很有必要,就是在迭代或者更换接口的时候很有好处

备注:

参加其他各大服务商提供出来的api接口,对比了一下,种类很多如下,目测没有什么规律可言,如果你有好的资料或者想法请留言交流哈。

http://japi.juhe.cn/enterprise/getDetailByName

http://v.juhe.cn/shortLetter/submitTpl.php

http://op.juhe.cn/jiepai/oilcardrr/onlineorder

http://api.juheapi.com/japi/beian

https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list

https://api.weixin.qq.com/cgi-bin/clear_quota
       

  

     

猜你喜欢

转载自blog.csdn.net/mazhaer/article/details/81201590