Introduction of REST & RESTful WebServices Samuel Gao / [email protected] / December, 2007 Table of Contents u Network-based Architectural Styles 1. What is an Architectural Styles 2. Common architectural styles for network-based software u REpresentational State Transfer (REST) 1. Deriving REST 2. Knowing REST 3. Advantages of REST 4. REST and other concepts u RESTful Web Services u Writing REST services What is an Architectural Styles An architectural style is a coordinated set of architectural constraints that restricts the roles/features of architectural elements and the allowed relationships among those elements within any architecture that conforms to that style.[1] Common architectural styles for network-based software • Data-flow Styles – Pipe and Filter (PF) – Uniform Pipe and Filter (UPF) • Replication Styles – Replicated Repository (RR) – Cache ($) • Hierarchical Styles – Client-Server (CS) – Layered System (LS) and Layered-Client-Server (LCS) – Client-Stateless-Server (CSS) – Client-Cache-Stateless-Server (C$SS) – Layered-Client-Cache-Stateless-Server (LC$SS) – Remote Session (RS) – Remote Data Access (RDA) • Mobile Code Styles – Virtual Machine (VM) – Remote Evaluation (REV) – Code on Demand (COD) – Layered-Code-on-Demand-Client-Cache-Stateless-Server (LCODC$SS) – Mobile Agent (MA) Deriving REST 1. Starting with the Null Style
2. Client-Server 3. Stateless
4. Cache
5. Uniform Interface
6. Layered System
7. Code-On-Demand 8. Style Derivation Summary
Knowing REST 1. What’s REST – REST: REpresentational State Transfer – It’s an architecture style (the stateless client-server architecture), not a toolkit. 2. REST defined – Resources are identified by uniform resource identifiers (URIs) – Resources are manipulated through their representations – Messages are self-descriptive and stateless – Multiple representations are accepted or sent – Hypertext is the engine of application state 3 Representation A web page is a representation of a resource, and the customer can get the resource from the URLs. Clients can request a specific representation of the concept from the representations the server makes available. 4. State “State” means application/session state, maintained as part of the content transferred from client to server back to client. It is necessary to understand the context of an interaction. The "stateless" constraint means that all messages must include all application state. 5. Transfer of state • Connectors (client, server, cache, pipe, and filter) are unrelated to sessions • State is maintained , transferred from clients to servers and back to clients Advantages of REST • It uses well documented, well established, well used technology and methodology. • It's already here today; in fact it's been here for the last 12 years! • Resource centric rather than method centric. • Given a URI anyone already knows how to access it. • It's not another protocol on top of another protocol on top of another protocol on top of... • The response payload can be of any format (some may call this a disadvantage, however the Web copes with it, it's just a case of defining the application grammar). • Uses the inherent HTTP security model, certain methods to certain URIs can easily be restricted by firewall configuration, unlike other XML over HTTP messaging formats. • REST makes sense, use what we already have; it is the next logical extension of the web. REST and other concepts 1. OOP on REST Every useful data object has an address; Resources themselves are the targets for method calls. 2. REST and RPC – REST is not RPC, RPC says "define some methods that do something" whereas REST says "define some resources and they will have methods". – Most RPC applications don't adhere to the REST philosophy. 3. POST on the REST attitude There are two views on POST, “POST lets you pass a whole lot of parameters and get something back, bypassing caches.” and “POST lets you create new resources that are related to old ones.” The second view is on the REST point.
Comments:The main advantages of REST web services are: Lightweight - not a lot of extra xml markup Human Readable Results Easy to build - no toolkits required SOAP also has some advantages: Easy to consume - sometimes Rigid - type checking, adheres to a contract Development tools
RESTful Web Services In the web services world, REST is a key design idiom that embraces a stateless client-server architecture in which the web services are viewed as resources and can be identified by their URLs. Web service clients that want to use these resources access a particular representation by transferring application content using a small globally defined set of remote methods that describe the action to be performed on the resource. • When to use REST – The web services are completely stateless. – A caching infrastructure can be leveraged for performance. – The service producer and service consumer have a mutual understanding of the context and content being passed along. – Bandwidth is particularly important and needs to be limited. – Web service delivery or aggregation into existing web sites can be enabled easily with a RESTful style. • RESTful frameworks – Java API for XML Web Services (JAX-WS) – Rails – sqlREST – Restlet – Axis2 – REST-art – Tonic – Dbscript – Comments: Cetia4 REST Framework https://cetia4.dev./ XX Framework http://www. sqlREST http://sqlrest./ restlet http://www./ Axis2 http://ws./axis2/ REST-art http:///projects/rest-art CognitiveWeb http:///projects/cweb Konstrukt http://www./ Tonic http://tonic./ dbscript http:/// 幾種支持REST的Java框架 目前宣稱支持REST的Java框架包括以下這些: Restlet([url=http://www./]http://www./[/url]) Cetia4([url=https://cetia4.dev./]https://cetia4.dev./[/url]) Apache Axis2([url=http://http//ws./axis2/]http://http://ws./axis2/[/url]) sqlREST([url=http://sqlrest./]http://sqlrest./[/url]) REST-art([url=http://rest-art./]http://rest-art./[/url]) 以下對(duì)這些框架進(jìn)行了較為全面的分析,。 Restlet,最新版本1.0.1 特點(diǎn):完全拋棄了Servlet API,作為替代,,自己實(shí)現(xiàn)了一套API,。能夠支持復(fù)雜的REST架構(gòu)設(shè)計(jì)。 缺點(diǎn): 1. 雖然也可以運(yùn)行于Web容器中,,但是難以利用Servlet和JSP等資源。因?yàn)樾枰硗鈱W(xué)習(xí)一套API和概念,,學(xué)習(xí)成本比較高,。 2. 完全不支持服務(wù)器端的HTTP Session,強(qiáng)制完全基于無(wú)狀態(tài)服務(wù)器模型來(lái)做開(kāi)發(fā),。對(duì)于基于瀏覽器的應(yīng)用來(lái)說(shuō),,開(kāi)發(fā)難度較高。 3. 自身沒(méi)有包括與Spring的集成,,可以使用第三方代碼與Spring集成,,集成難度較大。 4. 文檔不是很豐富,,大多比較簡(jiǎn)短,,很多時(shí)候需要自己去讀代碼,或者到其wiki上去查找,。 5. 沒(méi)有內(nèi)建的國(guó)際化支持,。 優(yōu)點(diǎn): 1. 有內(nèi)建的HTTP認(rèn)證機(jī)制,不需要另外開(kāi)發(fā)安全機(jī)制,。 2. 靈活性較高,,支持更多的REST概念,支持透明的內(nèi)容協(xié)商,,適合于開(kāi)發(fā)更加強(qiáng)大的REST組件(不限于服務(wù)器端應(yīng)用),。 3. 零配置文件,全部配置通過(guò)代碼來(lái)完成,。 相關(guān)資源: 功能列表:[url=http://www./about/features]http://www./about/features[/url] 簡(jiǎn)介:[url=http://www./about/introduction]http://www./about/introduction[/url] 教程:[url=http://www./documentation/1.0/tutorial]http://www./documentation/1.0/tutorial[/url] FAQ:[url=http://www./about/faq]http://www./about/faq[/url] Cetia4,,最新版本1.0 特點(diǎn):基于Servlet API開(kāi)發(fā),,可以運(yùn)行于所有的Web容器中。 優(yōu)點(diǎn): 1. 可以充分利用Servlet API和JSP等資源,,需要額外學(xué)習(xí)的概念較少,,學(xué)習(xí)成本較低。 2. 對(duì)于傳統(tǒng)的Web應(yīng)用,,可以使用服務(wù)器端HTTP Session,;對(duì)于Web服務(wù)類應(yīng)用,不使用HTTP Session,,基于無(wú)狀態(tài)服務(wù)器模型做開(kāi)發(fā),。 3. 自身包括了對(duì)于Web MVC的支持,熟悉Web MVC框架的開(kāi)發(fā)者很容易理解,。還內(nèi)建了參數(shù)映射,、參數(shù)驗(yàn)證等等傳統(tǒng)Web MVC框架所支持的功能。 4. 內(nèi)建了自己特有的導(dǎo)航對(duì)象棧的概念,,對(duì)于支持傳統(tǒng)的Web應(yīng)用的開(kāi)發(fā)(基于瀏覽器的導(dǎo)航)非常有幫助,。 5. 提供了JSP標(biāo)簽庫(kù),對(duì)于傳統(tǒng)的基于HTML表單的Web開(kāi)發(fā)非常有幫助,。 6. 支持與SiteMesh相配合,,由SiteMesh來(lái)支持頁(yè)面布局的重用。 7. 內(nèi)建有與Spring的集成,,集成起來(lái)非常容易,。 8. 配置文件完全基于標(biāo)準(zhǔn)的web.xml,不需要額外的配置文件,。大量使用默認(rèn)配置,一般情況下足以滿足常見(jiàn)的需求,。 9. 擁有很好的文檔,。 10. 有內(nèi)建的國(guó)際化支持。 缺點(diǎn): 1. 沒(méi)有內(nèi)建的HTTP認(rèn)證機(jī)制,,需要自行開(kāi)發(fā)安全機(jī)制,。 2. 對(duì)于內(nèi)容協(xié)商的支持比較弱,僅支持HTML和XML格式的表現(xiàn),。需要加以擴(kuò)展才能支持其他格式的表現(xiàn),。 相關(guān)資源: 教程:[url=https://cetia4.dev./files/documents/5545/38989/cetia4_tutorial.pdf]https://cetia4.dev./files/documents/5545/38989/cetia4_tutorial.pdf[/url] Axis2,最新版本1.2 特點(diǎn):同時(shí)支持SOAP和REST風(fēng)格的Web Service,。 缺點(diǎn): 1. 僅僅支持GET與POST方法,。 2. 僅僅是以REST風(fēng)格暴露出Web服務(wù),數(shù)據(jù)格式仍然是包含SOAP封裝的XML,,不能使用更加有效的格式,。 3. 只支持同步的調(diào)用方式,。 4. 僅僅提供了以SOAP方式暴露Web服務(wù)的最小化的支持,不支持全面的REST架構(gòu)設(shè)計(jì),。 相關(guān)資源: 簡(jiǎn)介:[url=http://ws./axis2/1_2/rest-ws.html]http://ws./axis2/1_2/rest-ws.html[/url] sqlREST,,最新版本0.3.1 特點(diǎn): 1. 為任何可以通過(guò)JDBC訪問(wèn)的數(shù)據(jù)庫(kù)提供Web服務(wù)訪問(wèn)接口,自動(dòng)將REST風(fēng)格的HTTP請(qǐng)求轉(zhuǎn)換為相應(yīng)的數(shù)據(jù)庫(kù)SQL語(yǔ)句,,并將數(shù)據(jù)庫(kù)中的記錄編碼為XML格式傳給客戶端,。是REST風(fēng)格的HTTP請(qǐng)求到數(shù)據(jù)庫(kù)中的數(shù)據(jù)的直接映射。 2. 基于Servlet API開(kāi)發(fā),。 缺點(diǎn): 1. 因?yàn)槭?/span>REST風(fēng)格的HTTP請(qǐng)求到SQL語(yǔ)句的直接映射,,因此強(qiáng)制使用以SQL和關(guān)系數(shù)據(jù)庫(kù)為中心的數(shù)據(jù)建模設(shè)計(jì)方法,不支持面向?qū)ο蟮脑O(shè)計(jì),。靈活性很低,,難以實(shí)現(xiàn)較為復(fù)雜的業(yè)務(wù)邏輯。 2. 因?yàn)橘Y源的定義僅限于數(shù)據(jù)庫(kù)的表,,難以實(shí)現(xiàn)更高層次的抽象,,必然會(huì)導(dǎo)致非常細(xì)粒度的API。應(yīng)用的性能和可伸縮性都難以保證,。 相關(guān)資源: 教程:[url=http://sqlrest./5-minutes-guide.htm]http://sqlrest./5-minutes-guide.htm[/url] REST-art,,最新版本0.2 特點(diǎn):一個(gè)旨在替換復(fù)雜的SOAP框架的REST框架,用來(lái)作為替代SOAP方便地發(fā)布Web服務(wù)的工具,。不是基于Servlet API開(kāi)發(fā),。 缺點(diǎn): 1. 目前尚處于剛剛起步的階段,功能非常少,。 2. 不是基于Servlet API,,帶來(lái)了額外的學(xué)習(xí)成本。 Posted 10月 6, 2007 1,、REST開(kāi)發(fā)框架縱覽 轉(zhuǎn)自http://www./cn/pop/pop.jsp?InfoID=2158 REST的流行使得越來(lái)越多的框架開(kāi)始支持REST,,而歷史的原因使得它們各自具有不同的特點(diǎn)。 隨著SOA的興盛,,Web服務(wù)也開(kāi)始駛?cè)肓思铀侔l(fā)展的快車道,。2000年Roy Thomas Fielding博士一紙論文更是宣告了第二代Web Service的到來(lái),REST—表述性狀態(tài)轉(zhuǎn)移,,為我們構(gòu)建下一代高性能,、高可伸縮性、簡(jiǎn)單性,、可移植性,、可靠性的Web程序提供了一個(gè)架構(gòu)風(fēng)格上的準(zhǔn)則。Web是簡(jiǎn)單的,,Web更是可編程的,,REST利用簡(jiǎn)單的 HTTP,、URI標(biāo)準(zhǔn)和XML語(yǔ)言構(gòu)建起輕量級(jí)的Web服務(wù),從而大幅度地提升了開(kāi)發(fā)效率和程序性能,。 由于REST設(shè)計(jì)哲學(xué)變得越來(lái)越流行,,許多RESTful框架如雨后春筍般涌現(xiàn)出來(lái)。 Rails Ruby on Rails是新興的敏捷Web開(kāi)發(fā)框架,,在動(dòng)態(tài)語(yǔ)言Ruby的支持下,,Rails以新鮮的視角告訴我們Web開(kāi)發(fā)是簡(jiǎn)單而快樂(lè)的。Rails對(duì) RESTful Web Service的開(kāi)發(fā)作了極大的封裝和簡(jiǎn)化,,這對(duì)開(kāi)發(fā)人員來(lái)說(shuō)是一個(gè)強(qiáng)大的工具,。而且即將發(fā)布的Rails 2.0將全面基于REST。 Rails 將ActiveRecord Model抽象為資源,,比如http://www./books就是一個(gè)books資源抽象,,而URI就是該資源的地址和唯一標(biāo)識(shí)符。對(duì)該資源的CRUD是利用標(biāo)準(zhǔn)HTTP協(xié)議中的GET,、POST,、PUT和DELETE方法,見(jiàn)表1,。 對(duì)于Book這個(gè)Model我們只需在URL映射配置文件routes.rb中使用map.resources:books作資源聲明,,則Rails自動(dòng)為我們將表1中的URL和HTTP動(dòng)詞與 Action做映射。而且,,Rails提供的scaffold_resource這個(gè)簡(jiǎn)單的Generator,,可以讓我們使用一條簡(jiǎn)單的命令自動(dòng)完成上述工作,而且會(huì)為我們自動(dòng)生成頁(yè)面視圖,。 HTTP動(dòng)詞中的GET和POST是我們常用的,,但是PUT和DELETE方法卻很少見(jiàn)。由于目前流行的Web瀏覽器大多不支持PUT和DELETE方法,,所以Rails采用一個(gè)虛擬的:method參數(shù)來(lái)模擬HTTP中的GET,、 POST、PUT,、DELETE方法。從表1中我們可以看到,,PUT和DELETE實(shí)際上都分別用POST和GET來(lái)作為提交方法,。 事實(shí)上,當(dāng)我們使用Rails 頁(yè)面模板的輔助鏈接方法并設(shè)置:method參數(shù)時(shí),,Rails會(huì)自動(dòng)為我們生成一個(gè)form提交,,并加上一個(gè)名字為“_method”的hidden字段,“_method”字段的值根據(jù)模板輔助鏈接方法:method的值而分別被賦予PUT或DELETE,。這樣,,Rails就彌補(bǔ)了Web瀏覽器不支持 PUT和DELETE的缺點(diǎn),,讓Rails開(kāi)發(fā)人員透明地使用RESTful Web Service API。 另外,,Rails最新的SVN代碼庫(kù)里還增加了一個(gè)RESTful Web Service客戶端的封裝庫(kù):ActiveResource,。ActiveResource模塊是Rails的REST客戶端,它對(duì)開(kāi)發(fā)REST客戶端 Web Service程序提供了簡(jiǎn)化,。ActiveResource參考了Rails的ActiveRecord架構(gòu),,甚至連API幾乎都是照搬過(guò)來(lái),這對(duì)于熟悉ActiveRecord的開(kāi)發(fā)人員十分方便,。使用ActiveResource時(shí),,我們只需讓一個(gè)自定義的模型繼承自 ActiveResource::Base類,并在該模型中設(shè)置好self.site屬性作為你要訪問(wèn)的RESTful Web Service的URL,,就可以像使用ActiveRecord一樣通過(guò)find,、create、save,、destroy方法來(lái)執(zhí)行針對(duì)該資源的 GET,、CREATE、UPDATE和DELETE操作,。 Rails對(duì)REST開(kāi)發(fā)做出了最大的簡(jiǎn)化,,Rails即將發(fā)布的2.0版本將全面基于REST。作為一個(gè)敏捷的Web開(kāi)發(fā)框架,,Rails一直在不斷突破傳統(tǒng)的Web開(kāi)發(fā)模式,、尋找Web開(kāi)發(fā)領(lǐng)域的殺手級(jí)架構(gòu)和框架,并在不斷進(jìn)步和發(fā)展中得到了大家的認(rèn)可,。 Axis2 Apache Axis2是傳統(tǒng)的Java Web Service框架Axis的下一代版本,。從最初的Apache Axis和Apache SOAP到目前的Axis2,經(jīng)歷了大量變革和發(fā)展,。相對(duì)以前的版本,,Axis2更靈活、更高效,、更簡(jiǎn)單,。作為Java端官方和傳統(tǒng)Web Service框架,在REST與SOAP的硝煙彌漫,、戰(zhàn)火紛飛的狀況下,,Axis2嘗試同時(shí)支持SOAP和REST,采用了WSDL2.0中將REST 與Web服務(wù)結(jié)合的工作成果,。 同樣的商業(yè)邏輯,,Axis2同時(shí)提供WS-*風(fēng)格的接口和REST風(fēng)格的接口。除了支持WS-*規(guī)范外,,Axis2已經(jīng)可以配置作為REST容器來(lái)發(fā)送和接收RESTful Web Service請(qǐng)求和應(yīng)答,。這讓Axis2的靈活性,、易用性、安全性和可靠性等都得到大幅度地提升,。 在Axis2里啟用REST風(fēng)格的Web服務(wù)十分簡(jiǎn)單,,首先修改服務(wù)器端的axis2.xml配置文件,將enableREST參數(shù)的值設(shè)置為true,。 然后在Axis2的客戶端的使用中,,初始化一個(gè)Options對(duì)象,填入我們要訪問(wèn)的服務(wù)URL,、設(shè)置傳輸協(xié)議為HTTP,、設(shè)置打開(kāi)對(duì)REST支持等,然后將Options對(duì)象傳遞給一個(gè)Call對(duì)象,,最后調(diào)用 call.invodeBlocking方法,,并在該訪問(wèn)的參數(shù)里指定調(diào)用的服務(wù)方法即可。 Axis2會(huì)同時(shí)作為SOAP端點(diǎn)和REST端點(diǎn),,在服務(wù)端可以同時(shí)發(fā)布這兩種風(fēng)格的Web服務(wù),。在客戶端,如果接收到的消息的contentType為text/xml,并且沒(méi)有SOAP相關(guān)的Header的話,,這個(gè)消息就會(huì)當(dāng)作 RESTful消息來(lái)處理,,否則當(dāng)作SOAP消息來(lái)處理。 其他RESTful框架 除了Ruby on Rails和Axis2,,還有一些框架也紛紛提供了對(duì)REST的支持,。 Django是基于python語(yǔ)言的敏捷Web和Web服務(wù)開(kāi)發(fā)框架,它的設(shè)計(jì)與Rails十分類似,,只不過(guò)簡(jiǎn)化和封裝稍少一些,。 django -rest-interface是Google Code上的一個(gè)開(kāi)源項(xiàng)目,它給Django的REST開(kāi)發(fā)接口提供了簡(jiǎn)便的封裝,。最重要的是Collection類和XMLResponder類,,在 urls.py文件里使用Collection定義資源集合以及使用XMLResponder定義responder,并在urlpatterns中將該資源集合映射到某一url,,則該資源就以REST方式發(fā)布了Web服務(wù),,對(duì)該Web服務(wù)的實(shí)現(xiàn)細(xì)節(jié)如HTTP動(dòng)詞等都隱藏在Collection里。 Apache CXF提供了一套創(chuàng)建SOA服務(wù)的基礎(chǔ)設(shè)施,,利用Apache CXF可以輕松創(chuàng)建基于SOAP或REST的Web服務(wù),。CXF是基于兩大著名開(kāi)源項(xiàng)目Celtix和XFire發(fā)展起來(lái)的,它提供了對(duì)JAX-WS的全面支持,,并且還支持Binding以及Transport。 Restlet是基于Java的另一個(gè)輕量但健全的RESTful框架,,它的發(fā)展十分穩(wěn)定,,有一定的潛力,。Restlet支持所有REST概念(資源、展示,、連接器,、組件等),支持阻塞和非阻塞(NIO)模式,,支持logging (LogChainlet),、authentication(GuardChainlet)和cool URIs重寫(xiě)(RedirectRestlet)等。 另外Microsoft的Indigo/WCF平臺(tái)最近也加入了REST的集成,,這意味這Indigo中的WebService將不僅僅使用SOAP了,。 而JSR 311: JAX-RS則定義了RESTful Web Service的Java API規(guī)范,該規(guī)范基于JDK5的元數(shù)據(jù)語(yǔ)法,。 對(duì)比SOAP和WS-*規(guī)范,,REST對(duì)于簡(jiǎn)化Web服務(wù)開(kāi)發(fā)和消費(fèi)的意義是重大的,許多大型網(wǎng)站已經(jīng)開(kāi)始提供REST服務(wù),,如Amazon,、eBay、 Yahoo和Flickr,。在關(guān)注CRUD場(chǎng)景的,、面向數(shù)據(jù)的應(yīng)用以及簡(jiǎn)單的、沒(méi)有復(fù)雜安全要求的Web服務(wù)方面,,使用REST是明智的,,但是在需要事務(wù)、嚴(yán)密的安全性等高級(jí)應(yīng)用方面,,可能基于WS-*的方案會(huì)更勝任,。 目前實(shí)現(xiàn)的最好、開(kāi)發(fā)效率最高的REST框架就是Ruby on Rails了,。RoR在即將發(fā)布的2.0版本也將全面基于REST,,可見(jiàn)Rails的作者DHH對(duì)REST是多么看重。Axis2則是Java端老牌 Web服務(wù)方案,,但是Axis2支持的東西太多太雜,,用Axis2開(kāi)發(fā)REST應(yīng)用總覺(jué)得有點(diǎn)大材小用。Restlet則麻雀雖小,,五臟俱全,,輕量而簡(jiǎn)潔,還有完善的認(rèn)證支持,,很值得期待,。JAX-RS作為RESTful Web Service的Java API規(guī)范,又是基于Annotation語(yǔ)法來(lái)提高開(kāi)發(fā)效率,相信不久的將來(lái)會(huì)更好地支持REST,。 • Some samples – The sqlREST project – The cetia4 project – The Dbscript project – TTG Discussions RESTful web services References [1].Roy Thomas Fielding, Architectural Styles and the Design of Network-based Software Architectures [2]. http://www./articles/rest.html [3]. http://www.blog/heiyuchuanxia/archive/20 06/10/14/75126.html [4]. http://java./developer/technicalArticles/ WebServices/restful/ [5]. http://image2.360doc.com/DownloadImg/2008/11/26/56145_2005312_1.jpg http://ker./web/szolgaltatas/szoftver/tarolas?lap=23wcf247g07pfng9jtnwnov4aaa2hd9wo64.html&rekord=233847 http://www./files/Web-Gateway-for-REST-Web-Services.html http:///chat/10620151/item/ http://www./restgate/
|
|
來(lái)自: chanvy > 《我的圖書(shū)館》