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!
- CGI is not safe, so we have script languages
-
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)