REST
REST
具象状态传输(Representational state transfer,REST)是设计基于命名资源而非消息的松耦合应用程序的一种风格。
REST是一种软件架构风格。
REST 原则
为所有“事物”定义ID
将所有事物链接在一起
将所有事物链接在一起
客户端和服务器之间的交互在请求之间是无状态的。
分层系统:组件无法了解它与之交互的中间层以外的组件。
REST中的资源
RESTful服务中很重要的一个特性即是同一资源,多种展现(json,xml,html):
1、使用http request header: Accept
GET /user/123 HTTP/1.1
Accept: application/xml //将返回xml格式数据
GET /user/123 HTTP/1.1
Accept: application/json //将返回json格式数据
Accept: application/xml //将返回xml格式数据
GET /user/123 HTTP/1.1
Accept: application/json //将返回json格式数据
2、使用扩展名
/user/123.xml 将返回xml格式数据
/user/123.json 将返回json格式数据
/user/123.html 将返回html格式数据
/user/123.json 将返回json格式数据
/user/123.html 将返回html格式数据
3、使用参数
/user/123?format=xml //将返回xml数据
/user/123?format=json //将返回json数据
/user/123?format=json //将返回json数据
RESTful
是一个使用HTTP并遵循REST原则的Web服务。
WCF REST
如果有WCF的编程经验,WCF下的REST实现就很好理解了。
数据契约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace REST
{
[DataContract]
public class User
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace REST
{
[DataContract]
public class User
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
}
服务契约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace REST
{
[ServiceContract]
public interface IUserService
{
[OperationContract]
[WebGet(UriTemplate = "User/{id}", ResponseFormat = WebMessageFormat.Json)]
User GetUser(string id);
[OperationContract]
[WebGet(UriTemplate = "Add/{name}/{pass}", ResponseFormat = WebMessageFormat.Xml)]
int AddUser(string name, string pass);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace REST
{
[ServiceContract]
public interface IUserService
{
[OperationContract]
[WebGet(UriTemplate = "User/{id}", ResponseFormat = WebMessageFormat.Json)]
User GetUser(string id);
[OperationContract]
[WebGet(UriTemplate = "Add/{name}/{pass}", ResponseFormat = WebMessageFormat.Xml)]
int AddUser(string name, string pass);
}
}
对方法标识的UriTemplate 就是REST中所谓的对事物定义ID。ResponseFormat用来定义资源的展现形式。
实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace REST
{
public class UserService : IUserService
{
public User GetUser(string id)
{
return new User { Id = id, Name = "name" + id };
}
public int AddUser(string name, string pass)
{
return 1;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace REST
{
public class UserService : IUserService
{
public User GetUser(string id)
{
return new User { Id = id, Name = "name" + id };
}
public int AddUser(string name, string pass)
{
return 1;
}
}
}
EndPoint
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="REST.UserService">
<endpoint address=""
binding="webHttpBinding"
contract="REST.IUserService"
behaviorConfiguration="UserServiceBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="UserServiceBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="REST.UserService">
<endpoint address=""
binding="webHttpBinding"
contract="REST.IUserService"
behaviorConfiguration="UserServiceBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="UserServiceBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
访问:http://localhost:2845/UserService.svc/User/100
返回:{"Id":"100","Name":"name100"} //json
访问:http://localhost:2845/UserService.svc/Add/tenghoo/123
返回:<int>1</int> //xml
参考:
http://badqiu.iteye.com/blog/552806
http://www.ibm.com/developerworks/cn/web/lp/restandweb/
http://www.cnblogs.com/jillzhang/archive/2010/04/04/1704388.html
http://www.cnblogs.com/jillzhang/archive/2010/04/04/1704388.html
作者:青羽