RESTful架构理解
REST就是一种架构思想,目的是建立长期的,不会随着技术发展被快速淘汰的Web服务架构。
REST代表具象状态传输,这是一个网络化的超媒体应用程序架构风格,它主要是用于构建轻量级的,可维护和可伸缩的Web服务。基于REST的服务称为REST式服务。REST是不依赖于任何协议,几乎每一个REST式服务使用HTTP作为其底层协议。
每个系统使用资源。这些资源可以是图片、视频文件、Web页面、业务信息,或任何可以表示为一个基于计算机的系统。服务的目的是为其客户端提供一个窗口,这样他们可以访问这些资源。服务架构师和开发人员想要这个服务易于实现,可维护、可扩展和可伸缩的。
RESTful设计承诺等等。一般来说,基于rest的服务应该有以下属性和功能,我将详细说明:
特点
Representations(表示)
数据的表现形式JSON或者XML
depending on your requirement, you can decide to use JSON or XML
Messages(消息)
客户端与服务端的通信
Clients send a request to the server, and the server replies with a response. Apart from the actual data, these messages also contain some metadata about the message.
It is important to have some background about the HTTP 1.1 request and response formats for designing RESTful Web services
URIs(统一资源定位符)
服务器端的所有资源都有一个url,
REST requires each resource to have at least one URI.
A RESTful service uses a directory hierarchy like human readable URIs to address its resources.
This URL has following format:
Protocol://ServiceName/ResourceType/ResourceID
Here are some important recommendations for well-structured URIs:
-
Use plural nouns(使用名词复数) for naming your resources.
-
Avoid using spaces as they create confusion. Use an _ (underscore)(下划线) or – (hyphen)(连字符) instead.
-
A URI is case insensitive(不分大小写). I use camel case in my URIs for better clarity. You can use all lower-case URIs.
-
You can have your own conventions(自己的约定), but stay consistent throughout the service. Make sure your clients are aware of this convention.It becomes easier for your clients to construct the URIs programmatically if they are aware of the resource hierarchy and the URI convention you follow.
-
A cool URI never changes(URI不改变); so give some thought before deciding on the URIs for your service.
If you need to change the location of a resource, do not discard the old URI.
If a request comes for the old URI, use status code 300 and redirect the client to the new location. -
Avoid verbs for your resource names(避免使用动词命名资源) until your resource is actually an operation or a process. Verbs are more suitable for the names of operations.
For example, a RESTful service should not have the URIs http://MyService/FetcthPerson/1 or http://MyService/DeletePerson?id=1.
Uniform interface(统一的接口)
RESTful systems should have a uniform interface. HTTP 1.1 provides a set of methods,called verbs, for this purpose. Among these the more important verbs are:
GET,PUT,POST,DELETE
Stateless(无状态)
A RESTful service is stateless and does not maintain the application state for any client.
A request cannot be dependent on a past request and a service treats each request independently.
Links between resources(资源之间的联系)
nstead of dumping all these resources, you can list
the resources and provide links to them. Links help keep the representations small in size.
Caching(缓存)
Caching is the concept of storing the generated results and using the stored results
instead of generating them repeatedly if the same request arrives in the near future.
This can be done on the client, the server,or on any other component between them, such as a proxy server.
6个特征:
- 客户端服务器各司所职
- 服务器不存储状态
- 客户端可以使用缓存
- 接口统一(HTTP)
- 系统分层
- 客户端可按需下载代码
依靠以上6点可以很快理解为什么REST可以被认为是一种具有自主演化能力的架构。
4点原则:
1,使用明确的HTTP方法
- retrieve data,use GET
- create data,user POST
- udpate or change data use PUT
- delete data use DELETE
2,无状态
不要在服务器存储状态信息
3,暴露URIs的目录结构
4,传输XML,JSON或者这两个
总结:
综合上面的解释,我们总结一下什么是RESTful架构:
(1)每一个URI代表一种资源;
(2)客户端和服务器之间,传递这种资源的某种表现层;
(3)客户端通过四个HTTP动词,对服务器端资源进行操作,实现"表现层状态转化"。
参考资料:
http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/
[http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069]
(http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069)
http://www.ruanyifeng.com/blog/2011/09/restful.html