Who invented REST?
REST was defined by Roy Fielding, a computer scientist. He presented the REST principles in his PhD dissertation in 2000.
REST 可以看成一種服務(wù)器接口設(shè)計(jì)的模式pattern静稻。它的全稱是 REpresentational State Transfer斜姥。它的意思就是說喇闸,服務(wù)器根據(jù)前端的請求修赞,返回給可展示的資源狀態(tài)。例如你請求了Weibo一個(gè)用戶的信息稚配,那么服務(wù)器就應(yīng)該返回給你一個(gè)user畅涂,包含了姓名,賬號道川,關(guān)注人數(shù)等信息午衰。
相關(guān)名詞
Resource:資源可以是前端想要獲取的任何東西,一張圖片冒萄,一個(gè)文件臊岸,一個(gè)用戶的信息等等。
Collection:一系列的用戶宦言。
URL:Uniform Resource Locator扇单,指向一個(gè)資源的路徑商模。
為什么需要REST
如果在完全沒有系統(tǒng)學(xué)習(xí)過接口設(shè)計(jì)的情況下奠旺,讓你去設(shè)計(jì)一系列接口蜘澜,你會(huì)怎么設(shè)計(jì)?我想大部分人都會(huì)做出如下的設(shè)計(jì):
/addNewUser
/updateUser
/deleteUser
...
你或許會(huì)覺得這樣的設(shè)計(jì)很readable啊响疚,一看就知道是什么意思了鄙信。是的,可讀性或許確實(shí)是它的一個(gè)優(yōu)點(diǎn)忿晕,但同時(shí)不容易去維護(hù)你的接口装诡。原因是你需要想方設(shè)法去像一個(gè)這種名字,接口少的時(shí)候還好践盼,多了呢鸦采?如何解決接口重名的問題。
解決的方法就是舍棄這種依靠命名來分辨接口的辦法咕幻。REST提出了一個(gè)很好的解決辦法渔伯,依靠資源定位符和request方法來分辨要請求的資源以及要進(jìn)行的操作(CRUD)。
Constraints in REST
- 在 URL 中只能包含名詞肄程,不能包含動(dòng)詞锣吼,例如
/addNewUser
應(yīng)該改成/users
。并且名詞都是復(fù)數(shù)蓝厌,不能是單數(shù)玄叠。這很好理解,資源在后臺(tái)數(shù)據(jù)中都不是單個(gè)存在的拓提,就像用戶不可能只有一個(gè)一樣读恃。 - 一個(gè)URL動(dòng)作的定義取決于request的方法(GET,POST...)代态。具體請求方法對應(yīng)的含義如下:
a.GET
用來獲取數(shù)據(jù)狐粱,并且不產(chǎn)生副作用(side effects)。例如GET: /companies
應(yīng)該獲取所有的公司信息胆数。
b.POST
用來創(chuàng)建一個(gè)新的Resource肌蜻。post不是一個(gè)冪等操作,也就是說多刺post操作會(huì)產(chǎn)生不一樣的結(jié)果必尼。就如同創(chuàng)建多個(gè)用戶時(shí)蒋搜,每一個(gè)用戶都是不一樣(起碼id是不一樣的)。
所以使用POST: /companies
會(huì)創(chuàng)建一個(gè)新的公司判莉。
c.PUT
用來更新一個(gè)資源豆挽,或者創(chuàng)建一個(gè)資源,如果不存在的話券盅。
所以PUT: /companies/Amazon
會(huì)更新Amazon這家公司帮哈,或者先創(chuàng)建Amazon然后更新它。
put是一個(gè)冪等操作锰镀,意味著多次相同URL的PUT操作只會(huì)產(chǎn)生一樣的效果娘侍。就像更新一個(gè)用戶咖刃,使用相同的信息就算更新一萬次,結(jié)果也是一樣的憾筏。
d.DELETE
會(huì)從數(shù)據(jù)庫刪除該資源嚎杨。
HTTP Response Status Code
2XX (Success category)
這類Code代表操作成功。
- 200 OK:標(biāo)準(zhǔn)的成功狀態(tài)氧腰,一般適用于GET枫浙,PUT和POST。
- 201 Created:當(dāng)新資源創(chuàng)建成功的時(shí)候可以返回這個(gè)狀態(tài)古拴。例如POST箩帚。
- 204 Not Content:代表操作成功,但沒有需要返回的信息黄痪。例如DELETE膏潮,刪除完成之后不需要返回額外信息了。
3xx (Redirection Category)
- 304 Not Modified indicates that the client has the response already in its cache. And hence there is no need to transfer the same data again.
4xx (Client Error Category)
These status codes represent that the client has raised a faulty request.
- 400 Bad Request indicates that the request by the client was not processed, as the server could not understand what the client is asking for.
- 401 Unauthorized indicates that the client is not allowed to access resources, and should re-request with the required credentials.
- 403 Forbidden indicates that the request is valid and the client is authenticated, but the client is not allowed access the page or resource for any reason. E.g sometimes the authorized client is not allowed to access the directory on the server.
-
404 Not Found indicates that the requested resource is not available now.
410 Gone indicates that the requested resource is no longer available which has been intentionally moved.
5xx (Server Error Category)
- 500 Internal Server Error indicates that the request is valid, but the server is totally confused and the server is asked to serve some unexpected condition.
- 503 Service Unavailable indicates that the server is down or unavailable to receive and process the request. Mostly if the server is undergoing maintenance.