`
xinyangwjb
  • 浏览: 81564 次
  • 性别: Icon_minigender_1
  • 来自: 信阳
社区版块
存档分类
最新评论

RESTful初探之二(Off to the races:Building a RESTful API)

 
阅读更多
Imagine an online application that manages races in which contestants run various distances(such as the Chicago Marathon).The application manages races(or events)and the runners associated with them.And it reports a particular runner's time (how long it took to run the race) and rank(what place the runner finished in).The race-management company,Acme Racing,wants you to build a RESTful Web service that enables sponsors to create new races and racers for a particular race,and that can provide official results for a particular race.
想象一个管理很多选手跑很多种距离的比赛的在线应用,这个应用管理比赛和参赛的选手。他报道制定选手的时间和名次。Acme Racing公司希望创建一个RESTful的Web服务,能够支持为指定的比赛创建新比赛和选手,并且提供官方结果。

Acme Racing already has a legacy fat-client application that supports similar requirements and leverages a simple database along with a domain model.Thus,the job of exposing this functionality is all that's left to do.Remember that the beauty of REST it its implicit loose coupling with an underlying application. accordingly,your job,at the moment,isn't to worry about the data model or the technology associated with it-it's to construct a RESTful API that supports the company's requirements.
Acme Racing 公司有个遗留的富客户端应用支持类似需求,并利用一个简单的基于数据库的对象模型。我们的工作就是暴露这些功能。记住REST的优美之处在于隐含的低耦合的底层应用。
__________________________________________________________________________________

Race URIs(uniform resource identifies)统一资源标示
Acme Races would like sponsors to be able to:
     *View the details of existing races
     *Create new races
     *Update existing races
     *Delete races
Beacuse REST boils down to named resources,the API becomes a series of URI patterns,and the behavior associated with a resource is invoked via standard HTTP commands.
因为REST可归结于命名资源,API成为一系列的URI模式。并且通过标准的HTTP命令使行为联系资源被调用。

As you can see,the client's requirements map nicely to CRUD. And as you konw from Table1,REST supports CRUD via the HTTP POST,GET,PUT,and DELETE requests,respectively,Accordingly,a base RESTful URI that supports these requirements could be http://racing.acme.com/race.Note that in this case,race is the resource clients would work with.
用户的需求很好的映射为CRUD。REST通过HTTP POST,GET,PUT,and DELETE来支持CRUD。因此,一个基础的支持这些需求的RESTful URI是http://racing.acme.com/race。注意这种情况下,race是资源客户将操作的。

Invoking this URI with an HTTP GET would return a list of races.(Don't worry about the format of the response just yet.)To add a new race,you would invoke the same URI with an HTTP POST containing the appropriate information(for instance,an XML document containing required race information,such as name,date,and distance).
调用这个URI进行HTTP GET请求会返回一列races。(这时先别考虑如何格式化response)添加一个新的race,你要提交相同的URI,只需URI使用POST提交,并且包含一些适当的信息(这个实例,一个XML文件包含race需求的信息,如名称,时间和距离)。

For updating and deleting existing races,you would need to act on a particular instance of a race.Accordingly,individual races can be addressed with a URI of  http://racing.acme.com/race/race_id.In this case ,race_id represents a placeholder for any race identifier(such as 1 or 600 meter).Consequently,viewing an existing race instance would be an HTTP GET to that URI:updating or deleting a race would be a PUT or DELETE request,respectively.
修改和删除已经存在的races,你将需要按照具体的实例来处理race。因此,不同个体的races可以被URI定位为http://racing.acme.com/race/race_id。这种情况下,race_id代表了一个任何race标示的占位符。因此,查看的URI是GET,删除时DELETE,修改时PUT.

Acme would also like the service to let users view individual data for a particular runner in a particular race.They'd like their service to support:
     *Obtaining all runners for a particular race. This data should also include run times and ranks for a race that's already completed.
     *Creating one or more runners for a particular race.
     *Updating a runner's information(such as age)for a particular race.
     *Deleting a runner for aparticular race.
公司要求为用户提供这样的服务:查看指定race的指定runner。包括获得指定race所有runners的信息。为race创建runner,为race修改runner及删除信息。
Acme would also like the service to let users view individual data for a particular runner in a particular race.
公司要求为用户提供查看指定race指定runner的个人资料的服务。

Just as with races,applying RESTful URIs to runners associated with a race is a logical exercise.Viewing all runners for a particular race,for example,would be implemented via a GET request to http://racing.acme.com/race/race_id/runner.
RESTful URIs同样适用于关联race的runner。

Obtaining individual data for a runner in a race would be addressed as http://racing.acme.com/race/race_id/runner/runner_id.
得到race的runner的个人数据可以定位为..

Just like race_id,runner_id is a placeholder for the logical implementation of IDs,which could be numbers,names,alphanumeric combinations,and so on.
正如race_id,runner_is是逻辑实现的占位符,他们可以使数字,名称,字母数字组合等等。

Adding runners to a race would be a POST request to http://racing.acme.com/race/race_id/runner. Updating or deleting particular runners would be, respectively, PUT and DELETE requests to http://racing.acme.com/race/race_id/runner/runner_id.
Thus, these URIs (each supporting some or all of the four standard HTTP requests) capture Acme Racing's requirements:
/race
/race/race_id
/race/race_id/runner
/race/race_id/runner/runner_id
Remember, a particular URI can map to more than one HTTP verb (for instance, applying an HTTP GET to /race returns data; applying a POST with appropriate data creates data on the server). Accordingly, some HTTP commands wouldn't be implemented. For example, /race wouldn't support the DELETE command (Acme Racing wouldn't want to delete all races); /race/race_id could support the DELETE command because removing a particular instance of a race is a business requirement.

分享到:
评论

相关推荐

    Python Flask高级编程之RESTFul API前后端分离精讲第七章节

    Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API前后端分离精讲第六章节Python Flask高级编程之RESTFul API...

    ASP.NET MVC 4 and the Web API: Building a REST Service from Start to Finish

    本书《*** MVC 4 and the Web API: Building a REST Service from Start to Finish》专注于如何利用*** MVC 4框架结合Web API构建世界级的REST服务。该书深入详细地介绍了如何使用*** MVC 4和Web API构建先进的REST...

    thinkphp6 RESTful API开发

    thinkphp6 RESTful API开发 开发过程记录笔记 https://blog.csdn.net/weixin_41120504/article/details/115638094

    Python Flask高级编程之RESTFul API前后端分离精讲第二章节

    Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API前后端分离精讲第二章节Python Flask高级编程之RESTFul API...

    SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验

    SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验 本实验的主要目标是使用SpringBoot、Mybatis和CXF框架来实现Restful API和WebService API接口的大实验。下面是实验的详细介绍: 标题...

    k8s restful api 访问

    restful api访问k8s集群,增删改查信息。 需要预先创建访问权限的配置。 官网api文档 https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/ 下面罗列部分api curl -u admin:admin ...

    Python Flask高级编程之RESTFul API前后端分离精讲第一讲解

    Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask高级编程之RESTFul API前后端分离精讲Python Flask...

    RESTful API设计规范

    ### RESTful API设计规范详解 #### 一、RESTful简介 RESTful是一种广泛应用于Web服务的设计风格,全称为...此外,RESTful设计还支持缓存机制、统一接口等特性,这些都使得RESTful API成为构建现代Web服务的标准之一。

    RESTFul API 接口说明

    #### 二、RESTful API 动词与方法 RESTful API 中最基础的概念之一是HTTP动词,它们代表了客户端向服务器发起请求的不同类型。常见的HTTP动词及其含义如下: 1. **GET**: - **用途**:用于获取资源的信息。 - *...

    RESTfulAPI设计:RESTfulAPI设计基础.docx

    RESTfulAPI设计:RESTfulAPI设计基础.docx

    Building.RESTful.Python.Web.Services.epub

    and profile RESTful APIs with each of the frameworks Develop a complex RESTful API that interacts with a PostgreSQL database Add authentication and permissions to a RESTful API built in each of the ...

    RESTfulAPI设计:RESTfulAPI安全性设计.docx

    RESTfulAPI设计:RESTfulAPI安全性设计.docx

    .NET 作为客户端调用WEBAPI RESTFUL服务端以及如何开发RESTFUL服务端用于客户端调用

    本篇文章将详细探讨.NET作为客户端调用WebAPI RESTful服务端的方法,以及如何开发RESTFUL服务端以供客户端调用。 首先,让我们了解一下客户端如何使用.NET调用WebAPI RESTful服务端。这通常涉及以下几个步骤: 1. ...

    RestfulApi服务端.zip

    本项目提供了一个RestfulApi服务端的示例,帮助开发者了解如何构建这样的服务。 在RestfulApi服务端的实现中,通常会用到以下技术栈: 1. **服务器框架**:如Spring Boot或Express.js,它们为快速构建RESTful API...

    RestFul API 案例

    **RestFul API 知识点详解** REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,以数据为中心,强调资源的状态转移。RESTful API是遵循REST原则设计的Web服务接口,...

    c c++实现http服务 c c++开发restful api服务

    本文将深入探讨如何使用C和C++来实现HTTP服务,并开发RESTful API服务器。 首先,HTTP(超文本传输协议)是互联网上应用最为广泛的一种网络协议,用于从万维网服务器传输超文本到本地浏览器。它是一个基于请求与...

    RESTFUL API设置最佳实

    #### 二、RESTful API 的关键要求 设计 RESTful API 时,应遵循以下关键要求: 1. **遵循 Web 标准**:在适当的地方使用 Web 标准,如 HTTP 协议、状态码等,这有助于提高 API 的互操作性和可维护性。 2. **开发者...

    Spring Boot + Mybatis 整合实现RESTful API

    Spring Boot 整合 Mybatis 实现RESTful API ,具体可以查看博客: http://blog.csdn.net/yaozhiqi1905658804/article/details/70820892

    Python中Flask-RESTful编写API接口(小白入门)

    Flask-RESTful是在Flask之上构建的,它利用了Flask的路由系统,并在此之上添加了对RESTful API的额外支持。 通过以上的知识点,可以入门Flask-RESTful,编写基础的API接口。这些知识点对于初学者来说是构建Web API...

Global site tag (gtag.js) - Google Analytics