RESTful初探之三(Formating the resource)

本文探讨了RESTful API的设计理念,特别是如何通过XML格式来表示赛车比赛中的资源,包括比赛和参赛者的信息,并讨论了如何使用HTTP方法进行CRUD操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[size=large]Race URIs
The RESTful API you built in the preceding section for Acme Racing covers the network endpoints or URIs but not the resources.As far as REST is concerned,the format of the resources doesn't matter.as i mentioned earlier.You could pass XML or binary streams back and forth,for example.
之前的RESTful API只有网络端点或者URIs,但没有资源。跟资源格式化没有关系。你可以反复传递XML或者二进制流。

XML is arguably the lingua franca of machine-to-machine communication in the context of business transactions.so it makes sense to construct a series of XML documents that the RESTful service will support.The domain for racing is fairly simple,and you can use an existing data model,so the task of defining a few XML documents that represent races and runners is straightforward.
XML是机器通信的通用语言。所以RESTful支持XML文件。racing对象相当的简单,你可以用已存在的模型,所以直接定义XML文件来代表races和runners。

for instance,a race can be defined in XML as Listing1 :
XML实例如下
[/size]
<race name="Mclean 1/2 Marathon" date="2008-05-12" distance="13.1" id="1">
<uri>/races/1</uri>
<description/>
</race>


[size=large]Note that a <race> has an id and that Listing1 includes a URI as part of the definition of a race.This is a key aspect of REST and indeed,the Web-resources are related and should be linked together.Accordingly,a <race> always contains a <uri> element describing its RESTful representation.The XML in Listing1 is arguably the response of a GET request to /races/1.
注意<race>有一个id并且包含一个URI作为定义race的一部分。这是Web和resources关联一起的关键。因此,一个<race>必须包括一个URI。

To create a new race,you could omit the id aspect(because managing unique IDs is something the application you're building here controls).This implies you could exclude the <uri> element as well.Consequently,a POST request would look something like Listing2[/size]
<race name="Limerick 2008 Half" date="2008-05-12" distance="13.4">
<description>erin go braugh and have a good time!</description>
</race>

[size=large]创建一个新的race,你可以忽略id(因为id指定你已经在控制台创建的应用)。这暗示了你可以不要<uri>。所以POST请求的如Listing2[/size]

[size=large]What about runners? A runner is connected to race,right?So the <race>element supports holding one or more <runner> elements,as shown in Listing3
runners关联着race。所以<race>对象支持一个或多个<runner>元素。
[/size]
<race name="Limerick 200 Half" date="2008-05-12" distance="13.4" id="9">
<uri>races/9</uri>
<description>erin go braugh and have a good time!</description>
<runners>
<runner first_name="Linda" last_name="Smith" age="25" id="21">
<uri>/races/9/runner/21</uri>
</runner>
<runner first_name="Andrew" last_name="Glover" age="22" id="20">
<uri>/races/9/runner/20</uri>
</runner>
</runners>
</race>

[size=large]The XML document in Listing3,for example,is what would be returned via the URI /race/race_id/runner.The API also supports CRUD operations performed on a single runner via the URI /race/race_id/runner/runner_id.
Listing3将被归还通过URI /race/race_id/runner。
Accordingly,the XML for these CRUD actions looks like Listing4:
下面是删除的写法
[/size]
<race name="Mclean 1/2 Marathon" date="2008-05-12" distance="13.1" id="1">
<uri>/races1</uri>
<description />
<runner first_name="Andrew" last_name="Glover" age="32" id="1">
<uri>/races/1/runner/1</uri>
<result time="100.04" place="45" />
</runner>
</race>


[size=large]Note that if the race is already complete, a runner's results can be included in the XML document. Remember, using a POST request means creating a runner; consequently, the <runner> element's id attribute would not be present.[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值