springboot官方文档学习01 restful风格
Understanding REST
REST (Representational State Transfer) was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation. REST is an architectural style for designing distributed systems. It is not a standard but a set of constraints, such as being stateless, having a client/server relationship, and a uniform interface. REST is not strictly related to HTTP, but it is most commonly associated with it.
Principles of REST
Resources expose easily understood directory structure URIs.
资源地址可以简单理解为URIS的目录结构,uri:统一资源标识符
Representations transfer JSON or XML to represent data objects and attributes.
表现层通过传输json和xml来传输数据对象和关系
Messages use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE).
使用http方法(get post put delete)显式的传递消息
Stateless interactions store no client context on the server between requests. State dependencies limit and restrict scalability. The client holds session state.
在一次request间,服务器端不存储客户端信息,限制依赖状态和限制扩张性,客户端
HTTP methods
Use HTTP methods to map CRUD (create, retrieve, update, delete) operations to HTTP requests.
通过http方法操作增删改查
GET
Retrieve information. GET requests must be safe and idempotent, meaning regardless of how many times it repeats with the same parameters, the results are the same. They can have side effects, but the user doesn’t expect them, so they cannot be critical to the operation of the system. Requests can also be partial or conditional.
查询操作,安全且幂等
Retrieve an address with an ID of 1:
GET /addresses/1
POST
Request that the resource at the URI do something with the provided entity. Often POST is used to create a new entity, but it can also be used to update an entity.
新增或者更新
Create a new address:
POST /addresses
PUT
Store an entity at a URI. PUT can create a new entity or update an existing one. A PUT request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request.
Modify the address with an ID of 1:
PUT /addresses/1
Note: PUT replaces an existing entity. If only a subset of data elements are provided, the rest will be replaced with empty or null.
更新,新增
PATCH
Update only the specified fields of an entity at a URI. A PATCH request is neither safe nor idempotent (RFC 5789). That’s because a PATCH operation cannot ensure the entire resource has been updated.
更新
PATCH /addresses/1
DELETE
Request that a resource be removed; however, the resource does not have to be removed immediately. It could be an asynchronous or long-running request.
删除
Delete an address with an ID of 1:
DELETE /addresses/1
HTTP status codes
Status codes indicate the result of the HTTP request.
1XX - informational
2XX - success
3XX - redirection
4XX - client error
5XX - server error
Media types
The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request. The client may set Accept to application/json if it is requesting a response in JSON. Conversely, when sending data, setting the Content-Type to application/xml tells the client that the data being sent in the request is XML.
REST架构风格最重要的架构约束有6个:
客户-服务器(Client-Server)
通信只能由客户端单方面发起,表现为请求-响应的形式。
无状态(Stateless)
通信的会话状态(Session State)应该全部由客户端负责维护。
缓存(Cache)
响应内容可以在通信链的某处被缓存,以改善网络效率。
统一接口(Uniform Interface)
通信链的组件之间通过统一的接口相互通信,以提高交互的可见性。为了使用统一接口,REST又使用了一些约束:面向资源,资源有标识符URI,资源表述,一组受限且定义良好的资源操作等。
(1)面向资源的。从资源的角度思考,Web经常被称作是“面向资源的”,资源可以是抽象的;
(2)资源标识符。要使用一个资源,我们需要能够在网络上标识它,这就是URI,Uniform Resource Identifier统一资源标识符。URI在HTTP中对应于URL,资源与资源标识符是一对多关系
(3)资源表述。即资源的表现方式,也称为资源视图,如XML, JSON, HTML, MP3, JPEG等,资源与其表述是一对多关系。在HTTP中通过HTTP header Accept, Content-Type指定
(4)资源的操作方法。uniform interface,统一接口包含一组受限的定义良好的操作,由它们进行资源的访问和操作,统一接口独立于资源的URI。在HTTP协议中即为GET/PUT/POST等method,
这些动词都有一定的含义,不应该乱用,具体定义见RESTful HTTP的实践。另外还包括HTTP定义的响应状态集合,如200 OK, 201 Created等,客户端通过HTTP method,对服务器端资源进行操作,实现”表现层状态转化”。
REST(Representational State Transfer,表述性状态转移)是指:相互链接的资源通过交换代表资源状态的表述来进行通信。超链接说白了就是URI–统一资源标识符
分层系统(Layered System)
通过限制组件的行为(即,每个组件只能“看到”与其交互的紧邻层),将架构分解为若干等级的层。
按需代码(Code-On-Demand,可选)
支持通过下载并执行一些代码(例如Java Applet、Flash或JavaScript),对客户端的功能进行扩展。
最后说一下HTTP,及HTTP与REST的关系。HTTP即HyperText Transfer Protocol,翻译成“超文本转移协议”更准确。REST是用来指导HTTP/1.1协议设计的理论框架(也称为架构风格),后来Roy Fielding对这套理论框架进行了更为系统、严谨地阐述。
对于使用HTTP的人员来说,统一接口应该是我们理解和实践REST的关键,其它约束其实不必太关心
还没有评论,来说两句吧...