REST 
to 
RESTful Web Service
sj
2011/09/28
sj@toright.com
http://blog.toright.com
Representational State
Transfer
2000 年 Dr. Roy Fielding 提
出的博士論文 

RFC 2616 的基礎 / Web 的聖經
REST 不是標準或協定
REST 是一種軟體設計架構風格

http://www.techcn.com.cn/index.php?doc-view-146097.html
What is RESTful ??
美麗 (beauty) 的事物可以稱為 Beautiful
同理可證
設計為 REST 的系統我們就稱為 RESTful
導讀推薦文章
How I Explained REST to My Wife

作者:Ryan Tomayko
使用 Internet 是一件簡單且
直覺的動作
就像是豐富的名詞加上多態的動詞
REST 概念介紹
 
REST Constraints
● Client-Server
● Stateless - 無狀態設計
● Cacheable - 可實作快取
● Uniform Interface - 一致性的介面
○ Identification of resources - 唯一的資源識別
○ Manipulation of resources - 特定的操作方法
○ Self-descriptive messages - 訊息自我描述
○ Hypermedia as the engine of application state - !?
● Layered System - 層級式架構
● Code-On-Demand (optional) - 像是 JavaScript
REST Architectural Elements
● REST Data Elements
● REST Connectors
● REST Components
REST Data Elements
● Resources and Resource Identifiers
透過一個全域的資源識別命名來對應與實體 (Entity) 間的關
係。
ex: HTTP URL
● Representations
在超媒體網路中資源呈現的各種方式
ex: HTTP Content-Type
REST Connectors
● Client
ex: HTTP Client Library
● Server
ex: Apache API
● Cache
ex: Browser Cache, Network Cache
● Resolver
ex: DNS lookup
● Tunnel
ex: SOCKS, SSL
REST Components
● User Agent  
ex: Browser
● Origin Server
ex: Apache httpd, M$ IIS
● Gateway
● Proxy

Components 透過 Connectors 進行訊息交換
REST 到底是什麼!?

http://futbol1.io.ua/s381473/sistema_super_bystroy_podgotovki_futbolistov_vysshego_klassa
HTTP 本身就是 REST 的實作!

http://news.sina.com.tw/books/history/barticle/15368.html
RESTful Web Service
 
What is RESTful Web Service ??
● 符合 REST  Constraints 的 Web Service 設計

http://www.history.com/this-day-in-history/united-states-nicknamed-uncle-sam

Q : HTTP 不就是 REST 的實作了嗎??那到底該怎麼設計
RESTful Web Service ??
A : 只要善用 HTTP 就對了!!
傳統 Web Service 設計
● 設計書籍管理系統
● 新增、刪除、修改、查詢 - CRUD
● XML Web Service
Web Service 傳統設計 (1)
● Web Service 網址 /books
● 新增書籍
<action>create</action>
<book isbn="A0100">
    <title>I hate the REST</title>
</book>
Response : <status>ok|fail</status>
● 修改書籍
<action>edit</action>
<book isbn="A0100">
    <title>I hate REST very much</title>
</book>
Response : <status>ok|fail</status>
Web Service 傳統設計 (1)
● 刪除書籍
<action>delete</action>
<isbn>A0010</isbn>
Response : <status>ok|fail</status>
● 查詢書籍
<action>search</action>
<isbn>A0010</isbn>
Response : <status>ok|fail</status>
Web Service 傳統設計 (2) 改良!?
● 更直覺的網址、更簡易 XML 內文、更通用的回應格式
<response code="0" messgae="ok">
    <data/>
</respone>
● 新增書籍 /books/create
<book isbn="A0100">
    <title>I hate the REST</title>
</book>
● 修改書籍 /books/update
<book isbn="A0100">
    <title>I hate REST very much</title>
</book>
Web Service 傳統設計 (2) 改良!?
● 刪除書籍 /books/delete
<isbn>A0010</isbn>
● 查詢書籍 /books/get
<isbn>A0010</isbn>
Web Service 傳統設計 (3) SOAP
SOAP Request
<soapenv:Envelope>
  <soapenv:Body>
    <req:echo xmlns:req="http://xxx.com/MyService/">
      <req:category>classifieds</req:category>
    </req:echo>
  </soapenv:Body>
</soapenv:Envelope>
Web Service 傳統設計 (3) SOAP
SOAP Response
<soapenv:Envelope>
  <soapenv:Header>
    <wsa:ReplyTo>
      <wsa:Address>http://schemas...</wsa:Address>
    </wsa:ReplyTo>
    <wsa:From>
      <wsa:Address>http://xxx.com/MyService</wsa:Address>
    </wsa:From>
    <wsa:MessageID>28BC11433905662036</wsa:MessageID>
  </soapenv:Header>
  <soapenv:Body>
    <req:echo xmlns:req="http://xxx.com/MyService">
      <req:category>classifieds</req:category>
    </req:echo>
  </soapenv:Body>
</soapenv:Envelope>
引述:How I Explained REST to My Wife
很遺憾,多數人為了達到同樣的目標,忙著用不同的方法創造出
複雜且不易使用的設計。不但名詞 (Nouns) 不通用,且動詞
(Verbs) 也不多態。我們拋棄了過去幾十年的失敗經驗,再度重
蹈覆轍。我們使用 HTTP 不僅僅是幫助我們使用網路,而卻忽略
了 HTTP Simplicity 的設計初衷與理念,得到的卻是華而不實
而絢麗的工具。

http://bellsee2.blogspot.tw/2011/01/pagliacci.html
遵循 HTTP 設計 RESTful Web Service
● Nouns
為你的資源進行 URL 定義
● Verbs
選擇適合的 Method 動詞
GET, POST, PUT, DELETE
● Content Types
選擇 Content-type
ex: XML / JSON / HTML

http://www.onlamp.com/pub/a/onlamp/2008/02/19/developing-restful-web-services-in-perl.html
RESTful Web Service 規格
功能

URL

HTTP
Resquest
Method

新增

/books

POST

刪除

/books/{isbn} DELETE

修改

/books/{isbn} PUT

查詢

/books/{isbn} GET

<book isbn="A0100">
  <title>REST1</title>
</book>

Response

Status
Code
200|401

200|401|404
<book isbn="A0100">
  <title>REST2</title>
</book>

200|401|404

<book isbn="A0100"> 200|404
  <title>REST1</title>
</book>

Content-type:application/xml, allpication/json,
text/html
Conclusion
直覺、簡單、快速
再次實作 HTTP 已經擁有的功能是多餘的
善用 HTTP 就是實現 RESTful 的好方法
『簡單』才是系統設計最佳法則
RESTful Web Service Cases
Amazon AWS, eBay, Facebook,
Yahoo Web Service, Hadoop API,
Google Web Service...
and you !?

http://www.superhappiness.com/albert-einstein.html
References
● Ryan Tomayko - How I Explained REST to My Wife
● SOUL & SHELL BLOG - 淺談 REST 軟體架構風格 (Part.I)
– 從了解 REST 到設計 RESTful!
● SOUL & SHELL BLOG - 淺談 REST 軟體架構風格 (Part.
II) – 如何設計 RESTful Web Service?
● Architectural Styles andthe Design of Network-based
Software Architectures
The End
Thanks!

sj@toright.com
http://blog.toright.com

REST to RESTful Web Service

  • 1.
  • 2.
    Representational State Transfer 2000 年Dr. Roy Fielding 提 出的博士論文  RFC 2616 的基礎 / Web 的聖經
  • 3.
  • 4.
    What is RESTful?? 美麗 (beauty) 的事物可以稱為 Beautiful 同理可證 設計為 REST 的系統我們就稱為 RESTful
  • 5.
    導讀推薦文章 How I ExplainedREST to My Wife 作者:Ryan Tomayko
  • 6.
  • 7.
  • 8.
    REST Constraints ● Client-Server ●Stateless - 無狀態設計 ● Cacheable - 可實作快取 ● Uniform Interface - 一致性的介面 ○ Identification of resources - 唯一的資源識別 ○ Manipulation of resources - 特定的操作方法 ○ Self-descriptive messages - 訊息自我描述 ○ Hypermedia as the engine of application state - !? ● Layered System - 層級式架構 ● Code-On-Demand (optional) - 像是 JavaScript
  • 9.
    REST Architectural Elements ●REST Data Elements ● REST Connectors ● REST Components
  • 10.
    REST Data Elements ●Resources and Resource Identifiers 透過一個全域的資源識別命名來對應與實體 (Entity) 間的關 係。 ex: HTTP URL ● Representations 在超媒體網路中資源呈現的各種方式 ex: HTTP Content-Type
  • 11.
    REST Connectors ● Client ex:HTTP Client Library ● Server ex: Apache API ● Cache ex: Browser Cache, Network Cache ● Resolver ex: DNS lookup ● Tunnel ex: SOCKS, SSL
  • 12.
    REST Components ● UserAgent   ex: Browser ● Origin Server ex: Apache httpd, M$ IIS ● Gateway ● Proxy Components 透過 Connectors 進行訊息交換
  • 13.
  • 14.
    HTTP 本身就是 REST的實作! http://news.sina.com.tw/books/history/barticle/15368.html
  • 15.
  • 16.
    What is RESTful WebService ?? ● 符合 REST  Constraints 的 Web Service 設計 http://www.history.com/this-day-in-history/united-states-nicknamed-uncle-sam Q : HTTP 不就是 REST 的實作了嗎??那到底該怎麼設計 RESTful Web Service ?? A : 只要善用 HTTP 就對了!!
  • 17.
    傳統 Web Service設計 ● 設計書籍管理系統 ● 新增、刪除、修改、查詢 - CRUD ● XML Web Service
  • 18.
    Web Service 傳統設計(1) ● Web Service 網址 /books ● 新增書籍 <action>create</action> <book isbn="A0100">     <title>I hate the REST</title> </book> Response : <status>ok|fail</status> ● 修改書籍 <action>edit</action> <book isbn="A0100">     <title>I hate REST very much</title> </book> Response : <status>ok|fail</status>
  • 19.
    Web Service 傳統設計(1) ● 刪除書籍 <action>delete</action> <isbn>A0010</isbn> Response : <status>ok|fail</status> ● 查詢書籍 <action>search</action> <isbn>A0010</isbn> Response : <status>ok|fail</status>
  • 20.
    Web Service 傳統設計(2) 改良!? ● 更直覺的網址、更簡易 XML 內文、更通用的回應格式 <response code="0" messgae="ok">     <data/> </respone> ● 新增書籍 /books/create <book isbn="A0100">     <title>I hate the REST</title> </book> ● 修改書籍 /books/update <book isbn="A0100">     <title>I hate REST very much</title> </book>
  • 21.
    Web Service 傳統設計(2) 改良!? ● 刪除書籍 /books/delete <isbn>A0010</isbn> ● 查詢書籍 /books/get <isbn>A0010</isbn>
  • 22.
    Web Service 傳統設計(3) SOAP SOAP Request <soapenv:Envelope>   <soapenv:Body>     <req:echo xmlns:req="http://xxx.com/MyService/">       <req:category>classifieds</req:category>     </req:echo>   </soapenv:Body> </soapenv:Envelope>
  • 23.
    Web Service 傳統設計(3) SOAP SOAP Response <soapenv:Envelope>   <soapenv:Header>     <wsa:ReplyTo>       <wsa:Address>http://schemas...</wsa:Address>     </wsa:ReplyTo>     <wsa:From>       <wsa:Address>http://xxx.com/MyService</wsa:Address>     </wsa:From>     <wsa:MessageID>28BC11433905662036</wsa:MessageID>   </soapenv:Header>   <soapenv:Body>     <req:echo xmlns:req="http://xxx.com/MyService">       <req:category>classifieds</req:category>     </req:echo>   </soapenv:Body> </soapenv:Envelope>
  • 24.
    引述:How I ExplainedREST to My Wife 很遺憾,多數人為了達到同樣的目標,忙著用不同的方法創造出 複雜且不易使用的設計。不但名詞 (Nouns) 不通用,且動詞 (Verbs) 也不多態。我們拋棄了過去幾十年的失敗經驗,再度重 蹈覆轍。我們使用 HTTP 不僅僅是幫助我們使用網路,而卻忽略 了 HTTP Simplicity 的設計初衷與理念,得到的卻是華而不實 而絢麗的工具。 http://bellsee2.blogspot.tw/2011/01/pagliacci.html
  • 25.
    遵循 HTTP 設計RESTful Web Service ● Nouns 為你的資源進行 URL 定義 ● Verbs 選擇適合的 Method 動詞 GET, POST, PUT, DELETE ● Content Types 選擇 Content-type ex: XML / JSON / HTML http://www.onlamp.com/pub/a/onlamp/2008/02/19/developing-restful-web-services-in-perl.html
  • 26.
    RESTful Web Service規格 功能 URL HTTP Resquest Method 新增 /books POST 刪除 /books/{isbn} DELETE 修改 /books/{isbn} PUT 查詢 /books/{isbn} GET <book isbn="A0100">   <title>REST1</title> </book> Response Status Code 200|401 200|401|404 <book isbn="A0100">   <title>REST2</title> </book> 200|401|404 <book isbn="A0100"> 200|404   <title>REST1</title> </book> Content-type:application/xml, allpication/json, text/html
  • 27.
    Conclusion 直覺、簡單、快速 再次實作 HTTP 已經擁有的功能是多餘的 善用HTTP 就是實現 RESTful 的好方法 『簡單』才是系統設計最佳法則
  • 28.
    RESTful Web ServiceCases Amazon AWS, eBay, Facebook, Yahoo Web Service, Hadoop API, Google Web Service...
  • 29.
  • 30.
    References ● Ryan Tomayko- How I Explained REST to My Wife ● SOUL & SHELL BLOG - 淺談 REST 軟體架構風格 (Part.I) – 從了解 REST 到設計 RESTful! ● SOUL & SHELL BLOG - 淺談 REST 軟體架構風格 (Part. II) – 如何設計 RESTful Web Service? ● Architectural Styles andthe Design of Network-based Software Architectures
  • 31.