Intro to REST2. 大纲
? REST 介简
? REST Server 思想开发
? 有框架支持现 (Framework)
? 参考文献 & 相 源关资
3. REST 介简 # 1
? Web Service
? 根据 W3C 的定 ,义 Web service 当是一个 件系 ,用以支应 软 统
持网 不同机器的互 操作。络间 动
? Web Service 三 最普遍的 用手段种 调
? 程 程 用(远 过 调 RPC ): Web Service 提供一个分布式函数或方
法接 口供用 用户调
? 面向服 架 (务 构 SOA ):通 由消息 ,而不再是某个 作讯 驱动 动
(方法 用)调
? 表象化状 移态迁 ( REST ): Web Service 类似于 HTTP 或其他
类似 ,它 把接口限定在一 广 人知的 准 作中(比如协议 们 组 为 标 动
HTTP 的 GET 、 PUT 、 DELETE )以供 用调
4. ? Representational State Transfer
? 表象化状 移态迁
? Roy Fielding 博士在 2000 年他的博士 文中提出来的一 件架论 种软 构风
格
? REST 宗旨
? REST 从 源资 的角度来 察整个网 ,分布在各 的 源由观 络 处 资 URI 定,确
而客 端的 用通户 应 过 URI 来 取 源的表形。 得 些表形致使 些获 资 获 这 这
用程序 了其状 。应 转变 态
? 随着不断 取 源的表形,客 端 用不断地在 着其状 ,所 表获 资 户 应 转变 态 谓
形化的状 (态转变 Representational State Transfer )
REST 介简 # 2
5. REST 介简 # 3
Client
(Server)
Server
vps.mobroad.com/app
…
/orders/ { id }
POST / GET / PUT / DELETE
/customers/ { id }
POST / GET / PUT / DELETE
/customers/ { id } /orders
POST / GET / PUT / DELETE
Request: GET
http://.../customers/1/orders
Response:
Content-type: XML/JSON
http://.../orders/00131
http://.../orders/00832
http://.../orders/00759
? 以 品商店 例产 为
客 端 求列出户 请 id 为 1 的用 的所有户 订单
7. REST 介简 #5
? HTTP 交互
? CRUD : Create, Retrieve, Update, Delete
? Safe : 安全方法,是否改 服 器端的数据变 务
? Idempotent : 等方法,若干次串行 求是否与一次 求 生 果相同幂 请 请 产 结
Method CRUD Safe Idempotent
POST Create
update, delete X X
GET Retrieve
○ ○
PUT Update
create X ○
DELETE Delete
X ○
8. ? 取所有 品列表获 产
Request GET http://www.store.com/products
Response
<store>
<product>
<id>1</id>
<name>iPhone4</name>
</product>
<product>
<id>2</id>
<name>Mac Book Pro</name>
</product>
</store>
? 取某 一 品获 单 产
Request GET http://www.store.com/product/2
Response
<product>
<id>2</id>
<name>Mac Book Pro</name>
<price>$900</price>
<popularity>100%</popularity>
</product>
REST Client 思想开发 #1
? 添加一个新 品产
Request POST http://www.store.com/products
<product>
<name>iPad</name>
<price>$600</price>
<popularity>90%</popularity>
</product>
Response
http://www.store.com/product/3
? 更新某个指定 id 的 品产
Request PUT http://www.store.com/product/1
<product>
<price>$300</price>
</product>
Response
(only header)
? 除某 一 品删 单 产
Request DELETE http://www.store.com/product/2
Response
(only header)
11. 参考文献 & 相 源关资
? http://www.ics.uci.edu/~fielding/pubs/di
ssertation/rest_arch_style.htm
? “RESTful Web Services”; Leonard
Richardson, Sam Ruby; (First Edition)
May 2007
? http://www.wikipedia.org/
Editor's Notes #5: Roy Fielding, HTTP协议制定者之一