关于REST web service

来源:互联网 发布:淘宝手机触屏版 编辑:程序博客网 时间:2024/05/16 12:21

转自:

1. REST web service is a three-legged stool

2. 基于REST架构的Web Service设计

REST web service is a three-legged stool

 

The more I read and think about Rest web service, the more I am convinced that it is a three-legged stool. The three legs are: resources / noun, actions (get, post), and data output (plain text, xml, Jason).

For millions of regular users, REST is the web itself. We have been "getting, posting" since the web was born; for purpose-driven background XmlHttpRequest calls, REST is the service, pure data and transactions, stripped of other extraneous / superfluous dressed-ups.

On the implementation side, REST does not come in any pre-defined packages and has to be landed in any frameworks. It is indiscriminate of languages. It can be perl, python, php, asp, asp .net 1.x, 2.x, 3.x. It is an "architectural style" (whatever that means). The opposite of Soap service, which is rigidly defined and has a bloated set of rules and protocols.

Also the opposite of the heavy and verbose Soap service, REST is light and instantly to the point.

The three legs of the REST web service are:

  1. Nouns: each noun identify a type of resource.

For example:http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo&query=pizza&zip=94306&results=2

A lot of REST talks stress using Nouns and avoiding verbs on the URL side, for example:

use

http://www.parts-depot.com/parts/getPart?id=00345

    Note the verb, getPart. Instead, use a noun:

 

http://www.parts-depot.com/parts/00345

I do not why using getPart would be violating the principal. However, the noun url is a logical url not physical url,  In .net, we certainly can use URL rewriting to achieve this goal

2. Verbs (Get, Post ...).

With other unix-sort of languages, like perl, there are PUT, and Delete.

GET is obvious, it is the Select in database queries.

Why POST?

POST can be used to deal with large amount data,  or form data.


3. Output

The whole purpose of REST is this: data representation. It can have many formats: plain text (though not recommended), XML, JSON. The list could be ever expanding.

So REST is a conceptual idea. It is a style, not implementation. It is evolving as the web is evolving. Like the web, it has some design principals: nouns, verbs, output.

=========================================================

REST的架构设计

  REST(Representational State Transfer)是一种轻量级的Web Service架构风格,其实现和操作明显比SOAP和XML-RPC更为简洁,可以完全通过HTTP协议实现,还可以利用缓存Cache来提高响应速度,性能、效率和易用性上都优于SOAP协议。

  REST架构遵循了CRUD原则,CRUD原则对于资源只需要四种行为:Create(创建)、Read(读取)、Update(更新)和Delete(删除)就可以完成对其操作和处理。这四个操作是一种原子操作,即一种无法再分的操作,通过它们可以构造复杂的操作过程,正如数学上四则运算是数字的最基本的运算一样。

  REST架构让人们真正理解我们的网络协议HTTP本来面貌,对资源的操作包括获取、创建、修改和删除资源的操作正好对应HTTP协议提供的GET、POST、PUT和DELETE方法,因此REST把HTTP对一个URL资源的操作限制在GET、POST、PUT和DELETE这四个之内。这种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。

  REST的设计准则

  REST架构是针对Web应用而设计的,其目的是为了降低开发的复杂性,提高系统的可伸缩性。REST提出了如下设计准则:

  网络上的所有事物都被抽象为资源(resource);

  每个资源对应一个唯一的资源标识符(resource identifier);

  通过通用的连接器接口(generic connector interface)对资源进行操作;

  对资源的各种操作不会改变资源标识符;

  所有的操作都是无状态的(stateless)。

  使用REST架构

  对于开发人员来说,关心的是如何使用REST架构,这里我们来简单谈谈这个问题。REST不仅仅是一种崭新的架构,它带来的更是一种全新的Web开发过程中的思维方式:通过URL来设计系统结构。REST是一套简单的设计原则、一种架构风格(或模式),不是一种具体的标准或架构。REST有很多成功的使用案例,著名的Delicious和Flickr都提供基于REST风格的API使用,客户端调用也极其方便,下面是我用ASP写的一个很简单的REST举例,从中可以看出REST是多么的简单易用。

  客户端代码:

Private Function httpGet(url, method, data)
    Dim xmlhttp
    Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xmlhttp.open method, url + "?" + data, False
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
    xmlhttp.setRequestHeader "Content-Length", Len(data)
    xmlhttp.send (Null)
    If (xmlhttp.Status = 200) Then httpGet = xmlhttp.responseText
    Set xmlhttp = Nothing
End Function

Private Function httpPost(url, method, data)
    Dim xmlhttp
    Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xmlhttp.open method, url, False
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
    xmlhttp.setRequestHeader "Content-Length", Len(data)
    xmlhttp.send (data)
    If (xmlhttp.Status = 200) Then httpPost = xmlhttp.responseText
    Set xmlhttp = Nothing
End Function

Private Function httpPut(url, method, data)
    Dim xmlhttp
    Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xmlhttp.open method, url, False
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
    xmlhttp.setRequestHeader "Content-Length", Len(data)
    xmlhttp.send (data)
    If xmlhttp.Status >= 400 And xmlhttp.Status <= 599 Then
        response.write " Error Occurred : " & xmlhttp.Status & " - " & xmlhttp.statusText
    Else
        response.write xmlhttp.responseText
    End If
    If (xmlhttp.Status = 200) Then httpPut = xmlhttp.responseText
    Set xmlhttp = Nothing
End Function

Private Function httpDelete(url, method, data)
    Dim xmlhttp
    Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
    xmlhttp.open method, url + "?" + data, False
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
    xmlhttp.setRequestHeader "Content-Length", Len(data)
    xmlhttp.send (Null)
    If xmlhttp.Status >= 400 And xmlhttp.Status <= 599 Then
        response.write " Error Occurred : " & xmlhttp.Status & " - " & xmlhttp.statusText
    Else
        response.write xmlhttp.responseText
    End If
    If (xmlhttp.Status = 200) Then httpDelete = xmlhttp.responseText
    Set xmlhttp = Nothing
End Function

response.write httpPost("http://localhost/rest/service.asp", "POST", "do=POST")
response.write httpGet("http://localhost/rest/service.asp", "GET", "do=GET")
response.write httpPut("http://localhost/rest/service.asp", "PUT", "do=PUT")
response.write httpDelete("http://localhost/rest/service.asp", "DELETE", "do=DELETE")

  服务端代码:

Response.Write Request.ServerVariables("REQUEST_METHOD")
If (Request.ServerVariables("REQUEST_METHOD")="GET") Then
 Response.Write "DO GET" + Request("do")
ElseIf (Request.ServerVariables("REQUEST_METHOD")="POST") Then
 Response.Write "DO POST" + Request("do")
ElseIf (Request.ServerVariables("REQUEST_METHOD")="PUT") Then
 Response.Write "DO PUT" + Request("do")
ElseIf (Request.ServerVariables("REQUEST_METHOD")="DELETE") Then
 Response.Write "DO DELETE" + Request("do")
End if

  需要注意的是,IIS服务器默认是不支持ASP文件的PUT和DELETE操作,默认会返回“403 - Forbidden”错误,因此需要修改IIS的设置,修改方法是:管理根据-IIS信息服务器-网站-属性-主目录-应用程序配置-配置-映射,选择ASP - 编辑 - 修改为全部动作。

  关于更多关于REST方面的知识,建议阅读《RESTful Web Services》这本书。