• 理解 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;

  • 相关阅读:
    maven 常用命令
    navicat 破解
    linux命令
    Git常用命令
    关于近期工作的总结
    ES6新特性学习
    Hadoop初步学习
    串行、并行与并发的理解
    通过Spring profile方式实现多环境部署
    使用MySQL乐观锁解决超卖问题
  • 原文地址:https://www.cnblogs.com/johnblogs/p/6138487.html
Copyright © 2020-2023  润新知