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

RESTful初探之三(Formating the resource)

 
阅读更多
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实例如下

<race name="Mclean 1/2 Marathon" date="2008-05-12" distance="13.1" id="1">
     <uri>/races/1</uri>
     <description/>
</race>


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

<race name="Limerick 2008 Half" date="2008-05-12" distance="13.4">
 <description>erin go braugh and have a good time!</description>
</race>

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

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>元素。

<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>

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:
下面是删除的写法

<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>


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.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics