บทที่ 5 - Ttime.in.th...

7
1 ดร.ธีระยุทธ ทองเครือ ภาควิชาวิทยาการคอมพิวเตอร์ คณะวิทยาศาสตร์ มหาวิทยาลัยขอนแก่น บทที5 REST Web Services REST REST REST (REpresentational State Transfer) คือ สถาปัตยกรรมของระบบทีออกแบบสําหรับการให้บริการ โดยมองข้อมูลทุกอย างเป็น Resource ทีอ้างอิงได้ด้วย URI เว็บเซอร์วิสที่สร้างตามเงื่อนไขของ REST จะเรียกว "RESTful Web Services" มักถูกเรียกว Web API เช Facebook มีเว็บเซอร์วิสแบบ REST เรียกว Graph API REST ถูกนําเสนอโดยวิทยานิพนธ์ของนักศึกษาปริญญาเอก ชื่อ Roy Thomas Fielding ในปี .. 2000 2 REST REST เน้นความเรียบง ายในการเรียกใช้ผ าน URL บน HTTP เหมาะก บการให้บริการแบบ CRUD (Create, Read, Update, and Delete) อนุญาตให้แต ละ Resource มีรูปแบบข้อมูลได้หลากหลาย เช XML, JSON, TEXT, CSV เป็ นต้น ผู ้พัฒนาเว็บเซอร์วิสสามารถสร้างเว็บเซอร์วิสได้ง ายกว าแบบ SOAP ผู ้ใช้เว็บเซอร์วิสสามารถเรียกใช้เว็บเซอร์วิสได้ง ายกว าแบบ SOAP 3 ตัวอย่างเว็บเซอร์วิสแบบ REST ในปัจจ บัน ตัวอย่างเว็บเซอร์วิสแบบ REST ในปัจจ บัน Instagram API https://instagram.com/developer/ Google Maps API Web Services https://developers.google.com/maps/documentation/webservices/ Facebook Graph API https://developers.facebook.com/docs/graph-api Amazon Marketplace Web Service (Amazon MWS) https://developer.amazonservices.com/ 4

Transcript of บทที่ 5 - Ttime.in.th...

Page 1: บทที่ 5 - Ttime.in.th 5.pdfแนวโน้มของเว็บเซอร็์วสิ 9 SOAP Web Services (WS-*) REST Web Services เปรียบเทียบี

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

Page 2: บทที่ 5 - Ttime.in.th 5.pdfแนวโน้มของเว็บเซอร็์วสิ 9 SOAP Web Services (WS-*) REST Web Services เปรียบเทียบี

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

Page 3: บทที่ 5 - Ttime.in.th 5.pdfแนวโน้มของเว็บเซอร็์วสิ 9 SOAP Web Services (WS-*) REST Web Services เปรียบเทียบี

แนวโนมของเวบเซอรวสแนวโนมของเวบเซอรวส

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>

หรอ

Page 4: บทที่ 5 - Ttime.in.th 5.pdfแนวโน้มของเว็บเซอร็์วสิ 9 SOAP Web Services (WS-*) REST Web Services เปรียบเทียบี

ขนตอนการประมวลผลของเวบเซอรวส 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

Page 5: บทที่ 5 - Ttime.in.th 5.pdfแนวโน้มของเว็บเซอร็์วสิ 9 SOAP Web Services (WS-*) REST Web Services เปรียบเทียบี

รปแบบ 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>

หรอ

Page 6: บทที่ 5 - Ttime.in.th 5.pdfแนวโน้มของเว็บเซอร็์วสิ 9 SOAP Web Services (WS-*) REST Web Services เปรียบเทียบี

รปแบบผลลพธของ 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

Page 7: บทที่ 5 - Ttime.in.th 5.pdfแนวโน้มของเว็บเซอร็์วสิ 9 SOAP Web Services (WS-*) REST Web Services เปรียบเทียบี

การแกไขขอมลลกคาการแกไขขอมลลกคา

25

Request: PUT /customer/771 HTTP/1.1 Content-Type: application/xml

<customer id="771"> <firstname>Bill</firstname> <lastname>Buck</lastname>

</customer>

สงขอมลใหมทตองการนาไปแกไข