restful
TRANSCRIPT
RESTful 任我行RESTful 介绍及最佳实践
Medcl,2010.6.4
前情提要
1. REST 是什么?2. Why
RESTful ?3. RESTful.NET !
REST 、 RESTful ???
Roy Thomas Fielding 博士 2000 年提出 REST 是英文 Representational State
Transfer 的缩写 REST 是 WEB 服务的一种架构风格 REST 一种新型的分布式软件设计架构 使用 HTTP 、 URI 等广泛流行的标准和协议
REST 架构的主要原则
网络上的所有事物都可被抽象为资源( Resource )
每个资源都有一个唯一的资源标识符( Resource Identifier )
同一资源具有多种表现形式 (xml 、 json 等 ) 对资源的各种操作不会改变资源标识符 所有的操作都是无状态的( Stateless )
Ps :符合 REST 原则的架构方式即可称为 RESTful
资源、资源、资源
› 处处皆资源 (Resource) : 2010 公司的销售数据 小明 09 年一月份看过的书 正在上映的电影 公司销售的产品清单 … …
› REST 与资源› REST 面向的对象是资源
RE—— 表现,即 media type 的设计。 ST—— 通过跟踪超链接实现状态迁移。
资源与 URIDesign
RESTful 风格的一大特色就是使用 URI 来标示资源
URI 和 URL› http://example.com/users/
http://example.com/users/{user} (one for each user)
› http://example.com/findUserForm› http://example.com/locations/› http://example.com/locations/{location} (one
for each location)› http://example.com/findLocationForm
资源操作
http://example.com/users/› GET :获取一个资源
POST :创建一个新的资源 PUT :修改一个资源的状态 DELETE :删除一个资源
资源展现
Xml Json Xhtml RSS/Atom Other Media types
无状态性
› 无状态性使得客户端和服务器端不必保存对方的详细信息,服务器只
需要处理当前 Request ,而不必了解前面 Request 的历史
› 无状态性减少了服务器从局部错误中恢复的任务量,可以非常方便地
实现 Fail Over 技术,从而很容易地将服务器组件部署在集群内
› 无状态性使得服务器端不必在多个 Request 中保存状态,从而可以
更容易地释放资源
› 无状态性无需服务组件保存 Request 状态,因此可让服务器充分利
用 Pool 技术来提高稳定性和性能
RESTful 优缺点
SOAP 、 WSDL 、 WS*
SO WHY REST ?
RPC 样式的架构中,关注点在于方法,而在 REST 样式的架构中,关注点在于资源
REST
轻量级、 HTTP无状态请求可以由任何可用服务器回答,分布式、缓存、云计算资源唯一 URI 、标准接口基于成熟 HTTP 的安全模型Simplicity 、 human-friendly … …
REST 不是什么?
REST 不是技术 REST 不是 CRUD
RESTful.NET
REST 在 .NET 平台下的最佳实践
1. WCF RESTful Programming Model2. Hosting WCF RESTful Services3. Securing REST Endpoints
前提准备WCF REST Service Start Kit
visual studio 2008 sp1
.net framework 3.5 sp1
Fiddler (客户端调试)
URI
http://localhost:8080/rest/Userhttp://localhost:8080/rest/User/{userName}http://localhost:8080/rest/User/xml
/// <summary>/// 用户信息/// </summary>[DataContract(Name = "User")]public class UserInfo{
[DataMember]public string UserName;
[DataMember]public string Birthday;
}
添加
POST http://localhost:8080/rest/UserUser-Agent: FiddlerHost: localhost:8080Content-Length: 22Content-Type: application/json
{"userName":"Aobama"}
修改
PUT http://localhost:8080/rest/UserContent-Length: 53Content-Type: application/jsonHost: localhost:8080
{"user":{"Birthday":"1977\/1\/1","UserName":"medcl"}}
查询
http://localhost:8080/rest/User
http://localhost:8080/rest/User/admin
http://localhost:8080/rest/User/xml
http://localhost:8080/rest/Icon/