RESTful Web

Web & Web Service

Web Service
  • Normally:a method of communications over the WWW

  • W3C Definition:a software system

    • Designed to support interoperable machine-to-machine interaction
    • Over network
    • An interface with a certain format:WSDL
    • Other systems interact with the Web Service
The ways we are using Web Service
  • 远程过程调用(Remote Procedure Call, RPC)框架

    • 通过网络协议远程传递参数调用服务器端的函数(方法)

    • Strong Coupling —— 跟编程语言联系的比较紧密,强耦合性

    • Demo of RPC

        **Server端**         
        getUser()       
        addUser()
        removeUser()
        updateUser()
        getLocation()
        addLocation()
        removeLocation()
        updateLocation()
      
        **Client端**
        exampleAppObject = new ExampleApp("example.com:1234")
        exampleAppobject.getUser()
      
  • 面向服务的架构(Service-oriented Architecture, SOA)

    • 通过消息传递的方式
    • This is a good choice
  • 表述性状态传递(Representational State Transfer, REST)

    • Another choice

    • Demo of REST

        **Server端**         
        http://example.com/users/
        http://example.com/users/{user}
        http://example.com/findUserForm
        http://example.com/locations
        http://example.com/locations/{location}
        http://example.com/findLocationForm
      
        **Client端**
        userResource = new Resource(http://example.com/user/001)
        userResource.get()
      
Our Web
  • Human Web

    • For normal users
    • Website, Web Application, etc
  • Programmable Web

    • For program/programmer
    • A set of API
Web History
  • Stage1:Static Content

    • At the beginning of web
    • Full of static HTML content, which is the papers made by the scholar
    • Like a file sharing server supports Hypertext
    • Is that enough ? We want MORE !
  • Stage2:CGI Program

    • People wanted to have more function on the web
    • API appeared
    • CGI is made by C/C++, Perl, and some other language, which is powerful but a little tricky.
    • But, we want it better...
  • Stage3:Script Languages

    • CGI is not safe, so we have script languages
      • ASP, JSP, PHP, Java Applet, JavaScript, etc...
    • HTML was mixed with scripts, which is much more safe and powerful
    • Well, it was still triky for developers with pure scripts
      • The mixture of HTML and script was horrible when code increased
    • And, we want more!
  • Stage4:Thin Client

    • All the content was produced on the server
    • And we had MVC!
    • Back-end technology runs fast in this stage.
    • Still, we want more!
  • Stage5:丰富互联网程序(Rich Internet Applications, RIA)

    • Developers started to build single page application, which can be very useful on the desktop
    • Front-end technology started to run
      • Ajax, jQuery/jQuery UI, ExtJS, Prototype, etc...
      • Flex, Sliverlight, JavaFX
      • ......
    • Always, we want more!
  • Stage6:Mobile Application

    • RIA is great, we now have something greater —— mobile web
    • iOS, Android, Windows Phone, Blackberry, etc...
    • HTML5 + CSS3 + JavaScript
    • We are happy with it now, while we both know that's not enough
Web NOW !
  • Pure HTML is not enough
  • CGI and scripts suck
  • Developers, users, administrators want to be happy!
  • So, we have made our mind and technology improved:
    • Resource Oriented
    • HTTP 1.1:request method, status code, cache
    • REST
RESTful and Resource Oriented Architecture
  • REST:Representational State Transfer, 即即表述性状态传递

  • a style, not a standerd —— 是一种风格,而不是标准!

  • ROA:Resource Oriented Architecture, 即面向资源架构

    • What is resource:anything useful
    • Resources must have URIs
  • Use HTTP verbs to implement CRUD(=Create、Read、Update、Delete指数据库操作中的创建、读取、更新和删除)

  • Provide 2 kind of web service

    • Human Web
    • Programmable Web
HTTP Request Methods
  • Verbs:

    • HEAD
    • GET
    • POST
    • PUT
    • PATCH
    • DELETE
    • CONNECT
    • OPTIONS
    • TRACE

Details of REST Style

Build REST from Everything
  • There are two thoughts when we want build something

    • Nothing we have, we start from bringing in
    • Everything we have, we start from cutting down —— REST use this
  • For REST, the author made some cut off on the former web style

RESTful Binding
  • Client-Server

    • Separate the system into client and server
    • Client call request, send it to server
  • Stateless —— 无状态的

    • 客户端的请求是无状态的,服务器端只有在拆开请求报文得到其中的信息才知道是哪一个客户端发来的请求
    • Request shall contain all the information it needs
    • Server identify requests by the info it stored in, not some info on the server
  • Cacheable —— 可缓存的

    • 客户端可缓存的,要求客户端发出的每一个请求都要指明请求的数据在客户端是否可缓存
    • Data in the request should marked as either cacheable or not, in a sensitive or insensitive way, so that client can cache the data
  • Uniform Interface —— 统一接口

    • Optimize the structure,优化网络架构
    • Light coupling,减轻耦合性
  • Layered System —— 分层系统

    • Simplify the system
    • Make high performance possible
  • Code on Demand —— 可选代码

    • Expand system
REST Quick Tips
  • Use HTTP Verbs to mean something:GET, PUT, DELETE, POST
  • Provide sensible resource names
  • Use HTTP response code to indicate staus
  • Offer JSON and XML
Resource Naming
  • To name a resource to make it readable and programmable
  • 统一资源定位符 URI:the name of resource as well as an address on the web
  • A RESTful URI should refer to a resource that is a thing(n), not an action(v)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,905评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,140评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,791评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,483评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,476评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,516评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,905评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,560评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,778评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,557评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,635评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,338评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,925评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,898评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,142评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,818评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,347评论 2 342

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,351评论 0 23
  • 日念家人一好处,念力加持享幸福! 【念夫好】早上不到六点起床,不辞辛苦赶去早市去买新鲜的蔬菜!总是从点点滴滴关心着...
    风潇潇blj阅读 123评论 0 0
  • 「遇见」 白雪恋上山峰 想要白头 星星追逐白昼 不眠不休 故事结尾缠上开头
    wuli榛子酱阅读 237评论 0 0
  • 有关于《富士山下》,林夕讲过这样一段话,他说,其实你爱一个人,就像爱一座富士山,你有什么办法能让一座山据为己有?答...
    拾荒阅读 16,788评论 2 8
  • 绝望的边缘不是黑暗,而是白昼的来临
    MMXI阅读 142评论 0 2