View
2
Download
0
Category
Preview:
Citation preview
1
ดร.ธระยทธ ทองเครอภาควชาวทยาการคอมพวเตอร คณะวทยาศาสตร
มหาวทยาลยขอนแกน
บทท 5REST Web Services
RESTRESTREST (REpresentational State Transfer) คอ สถาปตยกรรมของระบบท
ออกแบบสาหรบการใหบรการ โดยมองขอมลทกอยางเปน Resource ทอางองไดดวย URI เวบเซอรวสทสรางตามเงอนไขของ REST จะเรยกวา "RESTful Web
Services"มกถกเรยกวา Web API เชน Facebook มเวบเซอรวสแบบ REST เรยกวา
Graph API REST ถกนาเสนอโดยวทยานพนธของนกศกษาปรญญาเอก ชอ Roy
Thomas Fielding ในป ค.ศ. 2000
2
RESTRESTเนนความเรยบงายในการเรยกใชผาน URL บน HTTPเหมาะกบการใหบรการแบบ CRUD (Create, Read, Update, and Delete)อนญาตใหแตละ Resource มรปแบบขอมลไดหลากหลาย เชน XML,
JSON, TEXT, CSV เปนตนผพฒนาเวบเซอรวสสามารถสรางเวบเซอรวสไดงายกวาแบบ SOAPผใชเวบเซอรวสสามารถเรยกใชเวบเซอรวสไดงายกวาแบบ SOAP
3
ตวอยางเวบเซอรวสแบบ REST ในปจจบนตวอยางเวบเซอรวสแบบ REST ในปจจบนInstagram APIhttps://instagram.com/developer/
Google Maps API Web Serviceshttps://developers.google.com/maps/documentation/webservices/
Facebook Graph APIhttps://developers.facebook.com/docs/graph-api
Amazon Marketplace Web Service (Amazon MWS) https://developer.amazonservices.com/
4
Distance Matrix APIDistance Matrix API
5
http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Khon+Kaen+University&destinations=Bangkok&language=th Geocoding APIGeocoding API
6
https://maps.googleapis.com/maps/api/geocode/json?address=ขอนแกน
Facebook Graph APIFacebook Graph API
7
แหลงรวบรวมเวบเซอรวสแบบ RESTแหลงรวบรวมเวบเซอรวสแบบ RESThttp://www.programmableweb.com/apis/directory
http://www.webservicelist.com/
http://free-web-services.com/
8
แนวโนมของเวบเซอรวสแนวโนมของเวบเซอรวส
9
SOAP Web Services (WS-*)
REST Web Services
เปรยบเทยบ WS-* และ RESTเปรยบเทยบ WS-* และ REST
WS-* RESTการเรยกเวบเซอรวส เรยกใชดวย SOAP Message เรยกใชดวย URLรปแบบขอมลทสงไปยงเวบเซอรวส
XML XML, JSON, Textผสรางเวบเซอรวสสามารถระบไดวาจะใหผใชสงขอมลมาใหในรปแบบใด
รปแบบผลลพธทตอบกลบ
XML XML, JSON, Text, CSVผใชสามารถเลอกรปแบบผลลพธทตองการได
URL ทใชตดตอ ม 1 URL มหลาย URL
10
การเรยกเวบเซอรวสการเรยกเวบเซอรวสSOAP Web Services
REST Web Serviceshttp://www.studentx.com/student/566003204-1
11
<soap:Envelope><soap:Body>
<web:getStudent><web:stdID>566003204-1</web:stdID>
</web:getStudent></soap:Body>
</soap:Envelope>
รปแบบผลลพธทตอบกลบรปแบบผลลพธทตอบกลบ
12
<soap:Envelope><soap:Body><getStudentResponse>
<student id="566003204-1"> <stdName>Robert</stdName> <gpa>3.06</gpa> <register> <subject sid="322433" grade="A" /> <subject sid="322236" grade="C" /> </register> </student>
</getStudentResponse></soap:Body>
</soap:Envelope>
{"id": "566003204-1","stdName": "Robert","gpa": 3.06,"register": [
{"sid": 322433,"grade": "A"
},{
"sid": 322236,"grade": "C"
}]
}
SOAP Web Services REST Web Services
<student id="566003204-1"><stdName>Robert</stdName><gpa>3.06</gpa><register>
<subject sid="322433" grade="A" /><subject sid="322236" grade="C" />
</register> </student>
หรอ
ขนตอนการประมวลผลของเวบเซอรวส RESTขนตอนการประมวลผลของเวบเซอรวส REST
13
1. เตรยมขอมลท Service ตองการ2. สงขอมลไปยง Service
1. รบขอมลทผใชสงมา2. ประมวลผล3. สรางขอความตอบกลบ
ServiceConsumer
ServiceProvider
Request
Response
Consumer Processing Service Processing
http://www.abc.com/customer
วธการตดตอกบ REST Web Servicesวธการตดตอกบ REST Web Services
14
HTTP Method Database Operation การทางานGET SELECT ขอขอมลรายการเดยว
หรอหลายรายการPOST INSERT เพมขอมล หรอแกไขโดย
ทไมร IDPUT UPDATE แกไขขอมลโดยร IDDELETE DELETE ลบขอมล
ตวอยางระบบรบคาสงซอตวอยางระบบรบคาสงซอ
15
การอางอง Resource ของ RESTการอางอง Resource ของ RESTResource คอ ขอมลหรอผลลพธจากเวบเซอรวสแบบ RESTObject ตางๆ ของระบบจะกลายเปน Resource Order Customer Product
แตละ Resource จะม URI เปนของตนเอง
16
รปแบบ URIรปแบบ URI/orders เปน URI ทเปนตวแทนของทก order สามารถขอขอมล order และสราง order ไดจาก URI น
/orders/{id} เปน URI ทเปนตวแทนของ 1 order สามารถอาน แกไข และลบ order ไดจาก URI น {id} คอ รหส order
/orders/{id}/{lineitems} เปน URI ทระบรายการสนคาใน order
17
รปแบบ URIรปแบบ URIURI ของ Object อนๆ กมรปแบบเชนเดยวกน นกพฒนาสามารถกาหนดรปแบบ
ของ URI ไดเอง
/customers/customers/{id}
/products/products/{id}
18
ตวอยางตวอยางขอขอมลคาสงซอทงหมด http://localhost/rest/order
ขอขอมลคาสงซอทมรหสเปน 3655 http://localhost/rest/order/3655
ขอขอมลของลกคา พรอมกบแนบขอมลอนไปดวย http://localhost/rest/customer?gender=male&province=khonkaen
19
รปแบบผลลพธของ Customerรปแบบผลลพธของ Customer
20
{"id": 771,"firstname": "Bill","lastname": "Burke"
}
<customer id="771"> <firstname>Bill</firstname> <lastname>Burke</lastname>
</customer>
หรอ
รปแบบผลลพธของ Productรปแบบผลลพธของ Product
21
{"id": 543,"name": "iPhone","cost": 27000
}
<product id="543"> <name>iPhone</name> <cost>27000</cost>
</product>
หรอ
รปแบบผลลพธของ Orderรปแบบผลลพธของ Order
22
{"order_id": 133, "total": 27000, "date": "01/10/2015","customer" : {
"id": 771,"firstname": "Bill","lastname": "Burke"
},"items": [
{ "id": 543,"name": "iPhone","cost": 27000
}]
}
<order id="133"> <total>27000<total> <date>01/10/2015<date> <customer id="771"> <firstname>Bill</firstname> <lastname>Burke</lastname> </customer> <items>
<item> <product id="543"> <name>iPhone</name> <cost>27000</cost>
</item> </items>
</order>
หรอ
การขอขอมลลกคาการขอขอมลลกคา
23
Request: GET /customer/771 HTTP/1.1
Response: HTTP/1.1 200 OK Content-Type: application/xml
<customer id="771"> <firstname>Bill</firstname> <lastname>Burke</lastname>
</customer>
Request: GET /customer/771 HTTP/1.1
Response: HTTP/1.1 200 OK Content-Type: application/json
{"id": 771,"firstname": "Bill","lastname": "Burke"
}
การเพมขอมลลกคาการเพมขอมลลกคา
24
Request: POST /customers HTTP/1.1 Content-Type: application/xml
<customer> <firstname>Monica</firstname> <lastname>Burke</lastname>
</customer>
Response: HTTP/1.1 201 Created Location: http://example.com/customers/2322
การแกไขขอมลลกคาการแกไขขอมลลกคา
25
Request: PUT /customer/771 HTTP/1.1 Content-Type: application/xml
<customer id="771"> <firstname>Bill</firstname> <lastname>Buck</lastname>
</customer>
สงขอมลใหมทตองการนาไปแกไข
Recommended