一、 Understanding REST(了解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.
譯文:
2000年在羅伊菲爾丁的博士論文中引入和定義了REST。
REST是用于設計分布式系統的體系結構樣式,他不是一種标準,但是是一種規範,比如無狀态,但具有客戶機/伺服器關系,有一個統一的接口,但是REST不直接關聯HTTP。
二、Principles of REST(REST的設計原則)
原文:
Resources expose easily understood directory structure URIs.
Representations transfer JSON or XML to represent data objects and attributes.
Messages use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE).
Stateless interactions store no client context on the server between requests.
State dependencies limit and restrict scalability. The client holds session state.
譯文:
通過非常容易了解的URIs目錄結構去暴露資源。
一種傳輸JSON或者XML格式的資料去代表對象和屬性的表現形式。
消息明确地使用 HTTP 方法(例如:GET,POST, PUT, DELETE )。
在伺服器請求之間的無狀态互動存儲沒有用戶端環境。
三、HTTP methods(HTTP方法)
注意:REST服務中雖然建議使用HTTP協定中四種标準方法POST、DELETE、PUT、GET來分别實作常見的“增删改查”,
但實際中,建議直接用POST來實作“增改”,GET來實作“删查”即可(原因:DELETE和PUT甚至會被一些防火牆阻擋)。
Use HTTP methods to map CRUD (create, retrieve, update, delete) operations to HTTP requests.
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 idempotent.
Idempotency is the main difference between the expectations of PUT versus a POST request.
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.
譯文:狀态代碼表示HTTP請求的結果。
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.
譯文:
接受和Content-Type HTTP頭,可以用來描述發送的或者在HTTP請求中被請求的内容。
如果用戶端請求是一個JSON的響應類型,那麼用戶端也可以設定接收類型是JSON
原文官方網站:http://spring.io/understanding/REST點選打開連結