RESTAndroid技术知识首页推荐

RESTful架构理解

2016-08-29  本文已影响114人  大前端圈

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:

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个特征:

依靠以上6点可以很快理解为什么REST可以被认为是一种具有自主演化能力的架构。

4点原则:

1,使用明确的HTTP方法
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

上一篇下一篇

猜你喜欢

热点阅读