设为首页 加入收藏

TOP

restful架构(二)
2019-09-17 19:04:38 】 浏览:79
Tags:restful 架构
用户返回错误信息,一般来说,返回的信息中将error作为键名,出错信息作为键值即可

九、返回结果

针对不同操作,服务器向用户返回的结果应该符合以下规范

  • get /collection 返回资源对象的列表(数组)
  • get /collection/resource 返回单个资源对象
  • post /collection 返回新生成的资源对象
  • put /collection/resource 返回完整的资源对象
  • patch /collection/resource 返回完整的资源对象
  • delete /collection/resource 返回一个空文档

十、hypermedia api

restful api最好做到hypermedia,即返回结果中信息丰富一点,提供链接,连向其他api方法,使得用户不用去查询文档,也知道下一步应该做什么。比如当用户向api.test.com的根目录发出请求时,得到的文档包含以下结果

{
"link":{ "rel": "collection http://api.test.com/zoos",
     "href": "http://api/test.com",
     "title": "list of zoos",
     "type": "application/vnd.yourformat+json"
} }

 上面代码表示,文档中有一个link属性,用户读取这个属性就知道下一步该调用什么api,rel代表这个api与当前网址的关系(collection关系,并给出该collection网址),href表示api路径,title表示api的标题,type表示返回类型。

hypermedia api的设计也被称为HATEOAS,github的api就是这种设计,请求github的api会得到一个所有可用api网址列表

{
  "current_user_url": "https://api.github.com/user",
  "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",
  "authorizations_url": "https://api.github.com/authorizations",
  "code_search_url": "https://api.github.com/search/code?q={query}{&page,per_page,sort,order}",
  "commit_search_url": "https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}",
  "emails_url": "https://api.github.com/user/emails",
  "emojis_url": "https://api.github.com/emojis",
  "events_url": "https://api.github.com/events",
  "feeds_url": "https://api.github.com/feeds",
  "followers_url": "https://api.github.com/user/followers",
  "following_url": "https://api.github.com/user/following{/target}",
  "gists_url": "https://api.github.com/gists{/gist_id}",
  "hub_url": "https://api.github.com/hub",
  "issue_search_url": "https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}",
  "issues_url": "https://api.github.com/issues",
  "keys_url": "https://api.github.com/user/keys",
  "notifications_url": "https://api.github.com/notifications",
  "organization_repositories_url": "https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}",
  "organization_url": "https://api.github.com/orgs/{org}",
  "public_gists_url": "https://api.github.com/gists/public",
  "rate_limit_url": "https://api.github.com/rate_limit",
  "repository_url": "https://api.github.com/repos/{owner}/{repo}",
  "repository_search_url": "https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}",
  "current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}",
  "starred_url": "h
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇互联网移动业务服务端系统架构设.. 下一篇Spring Cloud微服务实战阅读笔记..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目