RESTful 簡介

1. 概念

RSEST 指的是一組架構約束條件和原則

RESTful 是滿足這些約束條件和原則的應用程序或設計

2. Web REST原則

Web 應用程序最重要的 REST 原則是,客戶端和服務器之間的交互在請求之間是無狀態的。從客戶端到服務器的每個請求都必須包含理解請求所必需的信息。
如果服務器在請求之間的任何時間點重啓,客戶端不會得到通知。所有資源都共享統一的接口,以便在客戶端和服務器之間傳輸狀態。
使用的是標準的 HTTP 方法,比如 GET、PUT、POST 和 DELETE。

3.REST基本操作

GET: 獲取對象的當前狀態
PUT: 改變對象的狀態
POST: 創建對象
DELETE:刪除對象
HEAD:獲取頭信息

4. curl 創建方法

創建索引庫: curl -XPUT 'http://10.10.10.46:9200/test/' curl -XPUT 'http://10.10.10.46:9200/test2/'
成功標誌:
[elk@localhost root]$ curl -XPUT 'http://10.10.10.46:9200/test/'
{"acknowledged":true,"shards_acknowledged":true,"index":"test"}
新增數據:
curl -H  "Content-Type: application/json"  -XPOST http://10.10.10.46:9200/test/user/1 -d '{"name" : "jack","age" : 28}'
curl -H  "Content-Type: application/json"  -XPUT http://10.10.10.46:9200/test/user/1 -d '{"name" : "jack","age" : 28}'
curl -H  "Content-Type: application/json"  -XPOST http://10.10.10.46:9200/test/user/1 -d '{"name" : "john"}'
curl -H  "Content-Type: application/json"  -XPOST http://10.10.10.46:9200/test/user/ -d '{"name" : "thmoas"}'
curl -H  "Content-Type: application/json"  -XPUT http://10.10.10.46:9200/test/user/2?op_type=create -d '{"name" : "lucy","age" : 28}'
curl -H  "Content-Type: application/json"  -XPUT http://10.10.10.46:9200/test/user/3/_create -d '{"name" : "lily","age" : 18}'

5.PUT和POST區別

創建操作都可以使用POST,也可以使用PUT
區別在於POST是作用在一個集合資源之上的;PUT操作是作用在一個具體資源之上的;
如果資源使用數據庫自增主鍵,沒有指明標識,這個時候必須使用POST

6.curl查詢方法

curl -XGET  
curl -XGET 'http://10.10.10.46:9200/test/user/1?_source=name&pretty' 
curl -XGET http://10.10.10.46:9200/test/user/_search?pretty

 7.DSL查詢

curl -H  "Content-Type: application/json"  -XPUT http://10.10.10.46:9200/test/user/4/_create -d '{"name" : "qiqi","age" : 18}'
curl -H  "Content-Type: application/json" -XGET http://10.10.10.46:9200/test/user/_search -d '{"query":{"match":{"name":"qiqi"}}}'

8.MGET查詢

curl -XPUT ' 
curl -H  "Content-Type: application/json"  -XPOST http://10.10.10.46:9200/test2/user/1 -d '{"name" : "Green","age" : 28}'
curl -H  "Content-Type: application/json" -XGET http://10.10.10.46:9200/_mget?pretty -d 
'{"docs":[{"_index":"test","_type":"user","_id":2,"_source":"name"},{"_index":"test2","_type":"user","_id":1}]}'
curl -H  "Content-Type: application/json" -XGET http://10.10.10.46:9200/test/user/_mget?pretty -d '{"docs":[{"_id":1},{"_id":2}]}'

9.更新操作(必須POST)

curl -H  "Content-Type: application/json"  -XPOST http://10.10.10.46:9200/test/user/1/_update -d '{"doc":{"name":"baby","age":18}}'

10.刪除操作

curl -XDELETE http://10.10.10.46:9200/test/user/1


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章