zoukankan      html  css  js  c++  java
  • (转)基于REST架构的Web Service设计

    原文出处:http://www.williamlong.info/archives/1728.html

    -----------------------------------------------------------------------割---------------------------------------------------------------- ---------------------

           先前我曾经介绍过利用Apache Axis实现基于SOAP的Web Service实现技术和相关代码,总的来说,SOAP的Web Service解决方案虽然较为成熟,且安全性较好,但是使用门槛较高,在大并发情况下会有性能问题,在互联网上使用不太普及,因此并不太适合Web 2.0网站服务使用,目前大量的Web 2.0网站使用另外一种解决方案——REST。

      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》这本书。

  • 相关阅读:
    synchronized使用及java中的原子性问题
    Volatile 原理及使用,java并发中的可见性问题
    final 修饰符
    java 常见OPTS参数的含义
    Redis面试题
    Count(1),Count(*),Count(column)区别
    Mysql索引创建及删除
    springboot 非端口模式启动
    sql批量插入缓慢
    sql server sql语句导入数据到execl2007中
  • 原文地址:https://www.cnblogs.com/voidobject/p/3975543.html
Copyright © 2011-2022 走看看