Introduction of REST & RESTful WebServices
Samuel Gao / sgao@ttg.cc / 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.
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
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.java.net/
XX Framework http://www.xxframework.org
sqlREST http://sqlrest.sourceforge.net/
restlet http://www.restlet.org/
Axis2 http://ws.apache.org/axis2/
REST-art http://sourceforge.net/projects/rest-art
CognitiveWeb http://sourceforge.net/projects/cweb
Konstrukt http://www.konstrukt.dk/
Tonic http://tonic.sourceforge.net/
dbscript http://dbscript.net/
几种支持REST的Java框架
目前宣称支持REST的Java框架包括以下这些:
Restlet([url=http://www.restlet.org/]http://www.restlet.org/[/url])
Cetia4([url=https://cetia4.dev.java.net/]https://cetia4.dev.java.net/[/url])
Apache Axis2([url=http://http//ws.apache.org/axis2/]http://http://ws.apache.org/axis2/[/url])
sqlREST([url=http://sqlrest.sourceforge.net/]http://sqlrest.sourceforge.net/[/url])
REST-art([url=http://rest-art.sourceforge.net/]http://rest-art.sourceforge.net/[/url])
以下对这些框架进行了较为全面的分析。
Restlet,最新版本1.0.1
特点:完全抛弃了Servlet API,作为替代,自己实现了一套API。能够支持复杂的REST架构设计。
缺点:
1. 虽然也可以运行于Web容器中,但是难以利用Servlet和JSP等资源。因为需要另外学习一套API和概念,学习成本比较高。
2. 完全不支持服务器端的HTTP Session,强制完全基于无状态服务器模型来做开发。对于基于浏览器的应用来说,开发难度较高。
3. 自身没有包括与Spring的集成,可以使用第三方代码与Spring集成,集成难度较大。
4. 文档不是很丰富,大多比较简短,很多时候需要自己去读代码,或者到其wiki上去查找。
5. 没有内建的国际化支持。
优点:
1. 有内建的HTTP认证机制,不需要另外开发安全机制。
2. 灵活性较高,支持更多的REST概念,支持透明的内容协商,适合于开发更加强大的REST组件(不限于服务器端应用)。
3. 零配置文件,全部配置通过代码来完成。
相关资源:
功能列表:[url=http://www.restlet.org/about/features]http://www.restlet.org/about/features[/url]
简介:[url=http://www.restlet.org/about/introduction]http://www.restlet.org/about/introduction[/url]
教程:[url=http://www.restlet.org/documentation/1.0/tutorial]http://www.restlet.org/documentation/1.0/tutorial[/url]
FAQ:[url=http://www.restlet.org/about/faq]http://www.restlet.org/about/faq[/url]
Cetia4,最新版本1.0
特点:基于Servlet API开发,可以运行于所有的Web容器中。
优点:
1. 可以充分利用Servlet API和JSP等资源,需要额外学习的概念较少,学习成本较低。
2. 对于传统的Web应用,可以使用服务器端HTTP Session;对于Web服务类应用,不使用HTTP Session,基于无状态服务器模型做开发。
3. 自身包括了对于Web MVC的支持,熟悉Web MVC框架的开发者很容易理解。还内建了参数映射、参数验证等等传统Web MVC框架所支持的功能。
4. 内建了自己特有的导航对象栈的概念,对于支持传统的Web应用的开发(基于浏览器的导航)非常有帮助。
5. 提供了JSP标签库,对于传统的基于HTML表单的Web开发非常有帮助。
6. 支持与SiteMesh相配合,由SiteMesh来支持页面布局的重用。
7. 内建有与Spring的集成,集成起来非常容易。
8. 配置文件完全基于标准的web.xml,不需要额外的配置文件。大量使用默认配置,一般情况下足以满足常见的需求。
9. 拥有很好的文档。
10. 有内建的国际化支持。
缺点:
1. 没有内建的HTTP认证机制,需要自行开发安全机制。
2. 对于内容协商的支持比较弱,仅支持HTML和XML格式的表现。需要加以扩展才能支持其他格式的表现。
相关资源:
教程:[url=https://cetia4.dev.java.net/files/documents/5545/38989/cetia4_tutorial.pdf]https://cetia4.dev.java.net/files/documents/5545/38989/cetia4_tutorial.pdf[/url]
Axis2,最新版本1.2
特点:同时支持SOAP和REST风格的Web Service。
缺点:
1. 仅仅支持GET与POST方法。
2. 仅仅是以REST风格暴露出Web服务,数据格式仍然是包含SOAP封装的XML,不能使用更加有效的格式。
3. 只支持同步的调用方式。
4. 仅仅提供了以SOAP方式暴露Web服务的最小化的支持,不支持全面的REST架构设计。
相关资源:
简介:[url=http://ws.apache.org/axis2/1_2/rest-ws.html]http://ws.apache.org/axis2/1_2/rest-ws.html[/url]
sqlREST,最新版本0.3.1
特点:
1. 为任何可以通过JDBC访问的数据库提供Web服务访问接口,自动将REST风格的HTTP请求转换为相应的数据库SQL语句,并将数据库中的记录编码为XML格式传给客户端。是REST风格的HTTP请求到数据库中的数据的直接映射。
2. 基于Servlet API开发。
缺点:
1. 因为是REST风格的HTTP请求到SQL语句的直接映射,因此强制使用以SQL和关系数据库为中心的数据建模设计方法,不支持面向对象的设计。灵活性很低,难以实现较为复杂的业务逻辑。
2. 因为资源的定义仅限于数据库的表,难以实现更高层次的抽象,必然会导致非常细粒度的API。应用的性能和可伸缩性都难以保证。
相关资源:
教程:[url=http://sqlrest.sourceforge.net/5-minutes-guide.htm]http://sqlrest.sourceforge.net/5-minutes-guide.htm[/url]
REST-art,最新版本0.2
特点:一个旨在替换复杂的SOAP框架的REST框架,用来作为替代SOAP方便地发布Web服务的工具。不是基于Servlet API开发。
缺点:
1. 目前尚处于刚刚起步的阶段,功能非常少。
2. 不是基于Servlet API,带来了额外的学习成本。
Posted 10月 6, 2007
1、REST开发框架纵览
转自http://www.swm.com.cn/cn/pop/pop.jsp?InfoID=2158
REST的流行使得越来越多的框架开始支持REST,而历史的原因使得它们各自具有不同的特点。
随着SOA的兴盛,Web服务也开始驶入了加速发展的快车道。2000年Roy Thomas Fielding博士一纸论文更是宣告了第二代Web Service的到来,REST—表述性状态转移,为我们构建下一代高性能、高可伸缩性、简单性、可移植性、可靠性的Web程序提供了一个架构风格上的准则。Web是简单的,Web更是可编程的,REST利用简单的 HTTP、URI标准和XML语言构建起轻量级的Web服务,从而大幅度地提升了开发效率和程序性能。
由于REST设计哲学变得越来越流行,许多RESTful框架如雨后春笋般涌现出来。
Rails
Ruby on Rails是新兴的敏捷Web开发框架,在动态语言Ruby的支持下,Rails以新鲜的视角告诉我们Web开发是简单而快乐的。Rails对 RESTful Web Service的开发作了极大的封装和简化,这对开发人员来说是一个强大的工具。而且即将发布的Rails 2.0将全面基于REST。
Rails 将ActiveRecord Model抽象为资源,比如http://www.example.com/books就是一个books资源抽象,而URI就是该资源的地址和唯一标识符。对该资源的CRUD是利用标准HTTP协议中的GET、POST、PUT和DELETE方法,见表1。
对于Book这个Model我们只需在URL映射配置文件routes.rb中使用map.resources:books作资源声明,则Rails自动为我们将表1中的URL和HTTP动词与 Action做映射。而且,Rails提供的scaffold_resource这个简单的Generator,可以让我们使用一条简单的命令自动完成上述工作,而且会为我们自动生成页面视图。
HTTP动词中的GET和POST是我们常用的,但是PUT和DELETE方法却很少见。由于目前流行的Web浏览器大多不支持PUT和DELETE方法,所以Rails采用一个虚拟的:method参数来模拟HTTP中的GET、 POST、PUT、DELETE方法。从表1中我们可以看到,PUT和DELETE实际上都分别用POST和GET来作为提交方法。
事实上,当我们使用Rails 页面模板的辅助链接方法并设置:method参数时,Rails会自动为我们生成一个form提交,并加上一个名字为“_method”的hidden字段,“_method”字段的值根据模板辅助链接方法:method的值而分别被赋予PUT或DELETE。这样,Rails就弥补了Web浏览器不支持 PUT和DELETE的缺点,让Rails开发人员透明地使用RESTful Web Service API。
另外,Rails最新的SVN代码库里还增加了一个RESTful Web Service客户端的封装库:ActiveResource。ActiveResource模块是Rails的REST客户端,它对开发REST客户端 Web Service程序提供了简化。ActiveResource参考了Rails的ActiveRecord架构,甚至连API几乎都是照搬过来,这对于熟悉ActiveRecord的开发人员十分方便。使用ActiveResource时,我们只需让一个自定义的模型继承自 ActiveResource::Base类,并在该模型中设置好self.site属性作为你要访问的RESTful Web Service的URL,就可以像使用ActiveRecord一样通过find、create、save、destroy方法来执行针对该资源的 GET、CREATE、UPDATE和DELETE操作。
Rails对REST开发做出了最大的简化,Rails即将发布的2.0版本将全面基于REST。作为一个敏捷的Web开发框架,Rails一直在不断突破传统的Web开发模式、寻找Web开发领域的杀手级架构和框架,并在不断进步和发展中得到了大家的认可。
Axis2
Apache Axis2是传统的Java Web Service框架Axis的下一代版本。从最初的Apache Axis和Apache SOAP到目前的Axis2,经历了大量变革和发展。相对以前的版本,Axis2更灵活、更高效、更简单。作为Java端官方和传统Web Service框架,在REST与SOAP的硝烟弥漫、战火纷飞的状况下,Axis2尝试同时支持SOAP和REST,采用了WSDL2.0中将REST 与Web服务结合的工作成果。
同样的商业逻辑,Axis2同时提供WS-*风格的接口和REST风格的接口。除了支持WS-*规范外,Axis2已经可以配置作为REST容器来发送和接收RESTful Web Service请求和应答。这让Axis2的灵活性、易用性、安全性和可靠性等都得到大幅度地提升。
在Axis2里启用REST风格的Web服务十分简单,首先修改服务器端的axis2.xml配置文件,将enableREST参数的值设置为true。
然后在Axis2的客户端的使用中,初始化一个Options对象,填入我们要访问的服务URL、设置传输协议为HTTP、设置打开对REST支持等,然后将Options对象传递给一个Call对象,最后调用 call.invodeBlocking方法,并在该访问的参数里指定调用的服务方法即可。
Axis2会同时作为SOAP端点和REST端点,在服务端可以同时发布这两种风格的Web服务。在客户端,如果接收到的消息的contentType为text/xml,并且没有SOAP相关的Header的话,这个消息就会当作 RESTful消息来处理,否则当作SOAP消息来处理。
其他RESTful框架
除了Ruby on Rails和Axis2,还有一些框架也纷纷提供了对REST的支持。
Django是基于python语言的敏捷Web和Web服务开发框架,它的设计与Rails十分类似,只不过简化和封装稍少一些。
django -rest-interface是Google Code上的一个开源项目,它给Django的REST开发接口提供了简便的封装。最重要的是Collection类和XMLResponder类,在 urls.py文件里使用Collection定义资源集合以及使用XMLResponder定义responder,并在urlpatterns中将该资源集合映射到某一url,则该资源就以REST方式发布了Web服务,对该Web服务的实现细节如HTTP动词等都隐藏在Collection里。
Apache CXF提供了一套创建SOA服务的基础设施,利用Apache CXF可以轻松创建基于SOAP或REST的Web服务。CXF是基于两大著名开源项目Celtix和XFire发展起来的,它提供了对JAX-WS的全面支持,并且还支持Binding以及Transport。
Restlet是基于Java的另一个轻量但健全的RESTful框架,它的发展十分稳定,有一定的潜力。Restlet支持所有REST概念(资源、展示、连接器、组件等),支持阻塞和非阻塞(NIO)模式,支持logging (LogChainlet)、authentication(GuardChainlet)和cool URIs重写(RedirectRestlet)等。
另外Microsoft的Indigo/WCF平台最近也加入了REST的集成,这意味这Indigo中的WebService将不仅仅使用SOAP了。
而JSR 311: JAX-RS则定义了RESTful Web Service的Java API规范,该规范基于JDK5的元数据语法。
对比SOAP和WS-*规范,REST对于简化Web服务开发和消费的意义是重大的,许多大型网站已经开始提供REST服务,如Amazon、eBay、 Yahoo和Flickr。在关注CRUD场景的、面向数据的应用以及简单的、没有复杂安全要求的Web服务方面,使用REST是明智的,但是在需要事务、严密的安全性等高级应用方面,可能基于WS-*的方案会更胜任。
目前实现的最好、开发效率最高的REST框架就是Ruby on Rails了。RoR在即将发布的2.0版本也将全面基于REST,可见Rails的作者DHH对REST是多么看重。Axis2则是Java端老牌 Web服务方案,但是Axis2支持的东西太多太杂,用Axis2开发REST应用总觉得有点大材小用。Restlet则麻雀虽小,五脏俱全,轻量而简洁,还有完善的认证支持,很值得期待。JAX-RS作为RESTful Web Service的Java API规范,又是基于Annotation语法来提高开发效率,相信不久的将来会更好地支持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.peej.co.uk/articles/rest.html
[3]. http://www.blogjava.net/heiyuchuanxia/archive/20 06/10/14/75126.html
[4]. http://java.sun.com/developer/technicalArticles/ WebServices/restful/
[5]. http://image2.360doc.com/DownloadImg/2008/11/26/56145_2005312_1.jpg
http://ker.eso.hu/web/szolgaltatas/szoftver/tarolas?lap=23wcf247g07pfng9jtnwnov4aaa2hd9wo64.html&rekord=233847
http://www.files-library.com/files/Web-Gateway-for-REST-Web-Services.html
http://riffs.com/chat/10620151/item/
http://www.osmoticweb.com/restgate/
联系客服