`
xinyangwjb
  • 浏览: 81804 次
  • 性别: 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.
分享到:
评论

相关推荐

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

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

    restful restful所需要的jar包

    restful restful所需要的jar包 ========================================= Restlet, a RESTful Web framework for Java ========================================= http://www.restlet.org -------------------...

    Django restful web services the easiest way to build python

    ### Django RESTful Web Services: The Easiest Way to Build Python #### 一、概述与背景 本书主要聚焦于如何利用Python及其最受欢迎的Web框架Django来构建RESTful Web服务。随着互联网技术的发展,RESTful Web...

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

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

    Restful C# 服务端篇之实现RestFul Service开发(简单实用)

    在IT行业中,RESTful(Representational State Transfer)是一种软件架构风格,用于设计网络应用程序,尤其在Web服务领域广泛应用。C#作为.NET框架的主要编程语言,提供了丰富的工具和技术来实现RESTful服务。本篇...

    C# 一个简单的 Restful 服务端和 Restful 客户端 Demo

    1. **RESTful原则**:REST(Representational State Transfer)的核心思想是资源(Resource)和状态转移。通过HTTP方法(GET、POST、PUT、DELETE等)来操作资源,实现无状态、缓存、层叠等特性。 2. **C#与ASP.NET ...

    谷歌浏览器restful请求插件

    总结来说,谷歌浏览器RESTful请求插件是开发人员必备的工具之一,尤其对于需要频繁与Elasticsearch交互的项目。通过这个插件,用户可以方便地发起REST请求,测试和调试API,同时学习和理解RESTful架构风格。无论是在...

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

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

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

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

    Building.RESTful.Python.Web.Services.epub

    Create web services that are lightweight, maintainable, scalable, and secure using the best tools and techniques designed for Python About This Book Develop RESTful Web Services using the most popular...

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

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

    thinkphp6 RESTful API开发

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

    restFul.Net

    在RESTful.NET中,`restService`是核心概念之一,它代表了一个服务端的资源处理单元。通过定义RESTful接口,开发者可以构建出易于理解、可维护的API。RESTful服务通常由一组URI(统一资源标识符)组成,每个URI对应...

    springmvc之restful风格CRUD

    7. **HATEOAS(Hypermedia as the Engine of Application State)**: 虽然不是强制性的,但HATEOAS是RESTful服务的一个重要特性,它使服务更加自我描述。在Spring HATEOAS库的帮助下,我们可以轻松地添加链接到...

    Java Restful Web 源代码,Java Restful Web 源代码

    Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web 源代码Java Restful Web...

    restfulFortune_restful_OpenGL3d_TheShow_wavefrontobject_

    1) how to make a Wavefront OBJ file that resembles a fortune cookie2) how to get fortunes using restful web reads3) how to create fortune cookie paper png image of text4) how to insert the paper image...

    c# 服务端调用RestFul Service的方法

    ### C# 服务端调用 RestFul Service 的方法 #### 概述 本文档将详细介绍如何使用 C# 创建和调用 RESTful 接口,包括 RESTful 的基本概念、如何构建 RESTful 风格的 API、服务端的具体实现步骤以及客户端如何调用...

    building restful web services with spring 5 2e

    Building RESTful Web Services with Spring 5 – Second Edition: Leverage the power of Spring 5.0, Java SE 9, and Spring Boot 2.0 Find out how to implement the REST architecture to build resilient ...

    restful接口文档模板

    ### RESTful接口文档模板知识点解析 #### 一、RESTful接口概述 REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,可以使用XML或者JSON格式传输数据,一般用于...

Global site tag (gtag.js) - Google Analytics