理解 RESTful WebService


RESTful 服務遵循REST(Representational State Transfer)的架構風格,中文翻譯為:表現層狀態轉化

對於所有的CRUD(Read/Create/Update/Delete),RESTFul架構基於HTTP的簡單動作(GET,POST,PUT,And DELETE)來實現。它簡單而且輕巧,比基於SOAP消息的WebService簡單的多的一種輕量級Web服務,RESTful WebService是沒有狀態的,發布和調用都非常的輕松容易。

表現層(Representation)

"資源"是一種信息實體,它可以有多種外在表現形式。我們把"資源"具體呈現出來的形式,叫做它的"表現層"(Representation)。

比如,文本可以用txt格式表現,也可以用HTML格式、XML格式、JSON格式表現,甚至可以采用二進制格式;

URI只代表資源的實體,不代表它的形式。URI應該只代表"資源"的位置。它的具體表現形式,應該在HTTP請求的頭信息中用Accept和Content-Type字段指定,這兩個字段才是對"表現層"的描述。

狀態轉化(State Transfer):

互聯網通信協議HTTP協議,是一個無狀態協議。這意味着,所有的狀態都保存在服務器端。因此,如果客戶端想要操作服務器,必須通過某種手段,讓服務器端發生"狀態轉化"(State Transfer)。而這種轉化是建立在表現層之上的,所以就是"表現層狀態轉化"。

客戶端用到的手段,只能是HTTP協議。具體來說,就是HTTP協議里面,四個表示操作方式的動詞:GET、POST、PUT、DELETE。它們分別對應四種基本操作:GET用來獲取資源,POST用來新建資源(也可以用於更新資源),PUT用來更新資源,DELETE用來刪除資源。

 

RESTful架構有一些典型的設計誤區:

1、URI包含動詞:因為"資源"表示一種實體,應該是名詞,URI不應該有動詞,動詞應該放在HTTP協議中;

2、URI中加入版本號:因為不同的版本,可以理解成同一種資源的不同表現形式,應該采用同一個URI。版本號可以在HTTP請求頭信息的Accept字段中進行區分;

 

例子:

1):創建Class Library Project,項目名為RestService

引用System.ServiceModel; System.ServiceModel.Web;

IRestServiceDemo代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace RestService
{
    [ServiceContract(Name = "RestSercice")]
    public interface IRestServiceDemo
    {
        [OperationContract]
        [WebGet(UriTemplate = Routing.GetClientRouting, BodyStyle = WebMessageBodyStyle.Bare)]
        string GetNameById(string id);

        [OperationContract]
        [WebGet(UriTemplate = Routing.GetClientRouting2, BodyStyle = WebMessageBodyStyle.Bare)]
        string GetAgeById(string id);
    }
    public static class Routing
    {
        public const string GetClientRouting = "/Client/{id}";
        public const string GetClientRouting2 = "/Client/zhj/{id}";
    }
}

RestService實現代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace RestService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode = ConcurrencyMode.Single,IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class RestServiceDemo:IRestServiceDemo
    {
        public string GetNameById(string id)
        {
            string returnStr = id + "name is John" ;
            return returnStr;
        }
        public string GetAgeById(string id)
        {
            string returnStr = id + "age is 18 years old";
            return returnStr;
        }
    }
}

2):創建Console Application項目,項目名為HostService

引用System.ServiceModel; System.ServiceModel.Web;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestService;
using System.ServiceModel.Web;

namespace HostService
{
    class Program
    {
        static void Main(string[] args)
        {
            RestServiceDemo restServiceDemo = new RestServiceDemo();
            WebServiceHost serviceHost = new WebServiceHost(restServiceDemo,new Uri("http://localhost:8000/MyService"));
            serviceHost.Open();
            Console.ReadKey();
            serviceHost.Close();
        }
    }
}

 

3):運行Host程序,在瀏覽器中輸入對應Service的Url

http://localhost:8000/MyService/Client/3   輸出:3 name is John;

http://localhost:8000/MyService/Client/zhj/3   輸出:3 age is 18 years old;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM