REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ...

30
ดร.ธีระยุทธ ทองเครือ ภาควิชาวิทยาการคอมพิวเตอร์ คณะวิทยาศาสตร์ มหาวิทยาลัยขอนแก่น ปฏิบัติการที10 REST Web Service Provider บน Google App Engine 1

Transcript of REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ...

Page 1: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

ดร.ธระยทธ ทองเครอ

ภาควชาวทยาการคอมพวเตอร คณะวทยาศาสตร

มหาวทยาลยขอนแกน

ปฏบตการท 10

REST Web Service Provider

บน Google App Engine

1

Page 2: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

Google App Engine (GAE)

GAE คอ แพลตฟอรมทใชสาหรบการพฒนาและตดตงเวบแอปพลเคชนบน Google

Data Center

GAE เปนแพลตฟอรมทเรยกวา PaaS (Platform as a Service) ซงเปนรปแบบบรการ

ชนดหนงบน Cloud Computing

GAE รองรบภาษาโปรแกรม PHP, Java, Python และ Go

2

Page 3: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

การสมครใชบรการ GAE

นกพฒนาจะตองม Google Account กอน หลงจากนนเขาสระบบการจดการท

https://appengine.google.com/

เมอ Login เขาสระบบแลวจะแสดงหนาเวบดงตวอยาง

3

Page 4: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

การสราง Application ใหมบน GAE

การสราง App ใหมจะได

Application ID ซงจะตองนาไปใชเมอ

จะสงโปรแกรมขน GAE

4

Page 5: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

การตดตง Google Plugin บน Eclipse

ทโปรแกรม Eclipse เลอกเมน Help > Install New Software... หลงจากนนใส URL

ในชอง Work with ดงน

https://dl.google.com/eclipse/plugin/4.4

เลอก Google Plugin for Eclipse

5

Page 6: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

การสราง RESTful บน GAE Project

สราง Project ใหม New > Other… > Google > Web Application Project

ใสชอ project และชอ package

Copy Library ของ Jersey ไวใน war/WEB-INF/lib

คลกขวาทชอ Project เลอก Properties > Java Build Path คลกทปม Add JARs… แล

เลอก ไฟล .jar ทงหมดใน war/WEB-INF/lib

สรางคลาสใหม package (ใชตวอยางจากไฟล Hello.java)

แกไขไฟล web.xml ซงอยใน war/WEB-INF

คลกขวาทชอ Project > Run As > Web Application

ทดสอบ http://localhost:8888/rest/hello

6

Page 7: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

ไฟล web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app>

<servlet><servlet-name>jersey-servlet</servlet-name><servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class><init-param>

<param-name>jersey.config.server.provider.packages</param-name><param-value>com.hello</param-value>

</init-param></servlet>

<servlet-mapping><servlet-name>jersey-servlet</servlet-name><url-pattern>/rest/*</url-pattern>

</servlet-mapping>

</web-app>

ระบชอ package ทสรางขน

ระบ path เรมตนของเวบเซอรวส

7

Page 8: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

Hello Service

@Path("/hello")public class Hello {

@GET@Produces("application/xml")public String getXml() {

return "<msg>Hello</msg>";}

}

ชอ Path เรมตนของเวบเซอรวส

เรยกโดยวธ GET

output มรปแบบเปน XML

ขอความทสงกลบ

8

Page 9: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

การ Deploy เวบเซอรวสขน GAE

คลกขวาทชอ Project (ตองเปน Google Project) เลอก Google > Deploy to App

Engine

จะมหนาใหกรอก username และ password เมอเขาสระบบแลวใหคลกทปม Accept

หนาตาง Deploy Project จะขนมา คลกท App Engine project settings… ใสชอ

Application ID และกดปม OK และคลกทปม Deploy

9

Page 10: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

ทดสอบการทางานของเวบเซอรวส

เขาไปท URL ของเวบเซอรวส

http://gae-app-1999.appspot.com/rest/hello

Application ID Path ทระบใน Annotation

10

Page 11: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

ทดสอบการทางานของเวบเซอรวส

11

Page 12: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

เวบเซอรวสใหบรการลกคา

Service ใหบรการ HTTP

Method

URL ในการเรยกเวบเซอรวส

รปแบบตามทกาหนดใน @Path

ขอมลเขา

@Consumes

ขอมลสงกลบ

@Produces

เกบขอมลลกคา POST http://localhost:8888/rest/customer ขอมลลกคา

รปแบบ JSON

รหสลกคาใหม

รปแบบ Text

ใหรายการขอมล

ลกคา

GET http://localhost:8888/rest/customer - ขอมลลกคา

รปแบบ JSON

ใหขอมลลกคาตาม

อเมล

GET http://localhost:8888/rest/customer/{email} - ขอมลลกคา

รปแบบ JSON

แกไขขอมลลกคา PUT http://localhost:8888/rest/customer/{email} ขอมลลกคา

รปแบบ JSON

ขอความ "success"

รปแบบ Text

ลบขอมลลกคา DELETE http://localhost:8888/rest/customer/{email} - ขอความ "success"

รปแบบ Text

12

Page 13: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

Google App Engine Datastore

GAE Datastore คอ ฐานขอมลแบบ NoSQL บน GAE

Entity หมายถง ตารางในฐานขอมลแบบ Relational Database

Property หมายถง ฟลดหรอคอลมนในฐานขอมลแบบ Relational Database

ตวอยางคาสงในการเพมขอมล

Entity customer = new Entity("Customer");customer.setProperty("fullname", "Theerayut Thongkrau");customer.setProperty("email", "[email protected]");customer.setProperty("age", "25");

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();datastore.put(customer);

สราง Entity

กาหนดคาลงไป

ในแตละ Property

นา Entity ทสรางขนเกบ

ลงฐานขอมล

ดขอมลเพมเตมท https://cloud.google.com/appengine/docs/java/datastore/ 13

Page 14: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

Service ใหบรการเกบขอมลลกคา@Path("/customer")public class Customer {

@POST@Consumes("application/json")public long add(String jsonString) {

System.out.println(jsonString);

// 1. ดงขอมลจาก JSON ดวย JSON PathObject document = Configuration.defaultConfiguration().jsonProvider().parse(jsonString);String fullname = JsonPath.read(document, "$.fullname");String email = JsonPath.read(document, "$.email");int age = JsonPath.read(document, "$.age");

// 2. สราง Entity และนาขอมลจาก JSON ไปเกบไวในแตละ PropertyEntity customer = new Entity("Customer");customer.setProperty("fullname", fullname);customer.setProperty("email", email);customer.setProperty("age", age);

// 3. เกบขอมลลงบนฐานขอมลDatastoreService datastore = DatastoreServiceFactory.getDatastoreService();datastore.put(customer);// 4. สงคา ID ของ Entity กลบไปใหผใช return customer.getKey().getId();

}}

ไฟล Customer.java

รบขอมลในรปแบบ

JSON ดวยวธ POST

14

Page 15: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

กจกรรม

copy ไฟล Customer.java ลงไปใน package com.hello

ทดสอบการทางานโดยปด Server แลวคลกขวาทชอ Project > Run As > Web

Application

ทดสอบเพมขอมลลกคาดวย SoapUI

URL: http://localhost:8888/rest/customer

Method: POST

ขอมลทสงไปยงเวบเซอรวส{

"fullname": "Peter","email": "[email protected]","age": 32

}

15

Page 16: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

กจกรรม

16

Page 17: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

กจกรรม

ดขอมลทเพมในฐานขอมลท http://localhost:8888/_ah/admin

17

Page 18: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

กจกรรม

ทาการ Deploy ขน Google App Engine

ทดสอบเพมขอมลลกคาดวย SoapUI

URL: http://[AppID].appspot.com/rest/customer

Method: POST

ขอมลทสงไปยงเวบเซอรวส{

"fullname": "Peter","email": "[email protected]","age": 32

}

18

Page 19: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

กจกรรม

19

Page 20: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

กจกรรม

ดขอมลทเพมในฐานขอมลท https://appengine.google.com/ เลอกชอ Application

เลอกเมน Datastore Viewer

20

Page 21: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

Service ใหรายการขอมลลกคา

ไฟล Customer.java

@GET@Produces("application/json")public String getCustomer() throws UnsupportedEncodingException {

// 1. ตดตอฐานขอมลDatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

// 2. ระบช อ Entity ทตองการ queryQuery query = new Query("Customer");

// 3. เร มดงขอมลจากฐานขอมลPreparedQuery pq = datastore.prepare(query);

// 4. สราง JSON Object และ JSON ArrayJsonObjectBuilder root = Json.createObjectBuilder();JsonArrayBuilder customers = Json.createArrayBuilder();

// 5. วนลปดงขอมลจากผลลพธของการ query เกบลง JSON Objectfor (Entity result : pq.asIterable()) {

JsonObjectBuilder customer = Json.createObjectBuilder();customer.add("fullname", result.getProperty("fullname").toString());customer.add("email", result.getProperty("email").toString());customer.add("age", result.getProperty("age").toString());customers.add(customer);

}root.add("customers", customers);// 6. แปลง JSON Object เปน String สงกลบไปใหผใช String result = new String(root.build().toString().getBytes("ISO8859_1"), "utf-8");return result;

}

สงผลลพธกลบใน

รปแบบ JSON

21

Page 22: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

ทดสอบการทางาน

22

Page 23: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

Service ใหขอมลลกคาตามอเมล

@GET@Produces("application/json")@Path("/{email}")public String getCustomerByEmail(@PathParam("email") String email) throws UnsupportedEncodingException {

// 1. ตดตอฐานขอมลDatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

// 2. ระบเงอนไขการคนหาในทน คนตาม property ชอ email ซงมคาเทากบคาท รบจาก URLFilter filter = new FilterPredicate("email", FilterOperator.EQUAL, email);

// 3. ระบช อ Entity ทตองการ queryQuery query = new Query("Customer").setFilter(filter);

// 4. เร มดงขอมลจากฐานขอมลPreparedQuery pq = datastore.prepare(query);

// 5. นา Entity ทคนหาเจอออกมาEntity customer = pq.asSingleEntity();

// 6. ดงขอมลจากผลลพธของการ query เกบลง JSON ObjectJsonObjectBuilder root = Json.createObjectBuilder();root.add("fullname", customer.getProperty("fullname").toString());root.add("email", customer.getProperty("email").toString());root.add("age", customer.getProperty("age").toString());

// 7. แปลง JSON Object เปน String สงกลบไปใหผใชString result = new String(root.build().toString().getBytes("ISO8859_1"), "utf-8");return result;

}ไฟล Customer.java

รบอเมลจาก URL และ

สงผลลพธกลบในรปแบบ JSON

23

Page 24: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

ทดสอบการทางาน

24

Page 25: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

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

ไฟล Customer.java

@PUT@Consumes("application/json")@Path("/{email}")public String edit(@PathParam("email") String email, String jsonString) {

// 1. นา String ทอยในรปแบบ JSON แปลงเปน ObjectObject document = Configuration.defaultConfiguration().jsonProvider().parse(jsonString);// 2. ดงขอมลดวย JSON PathString fullname = JsonPath.read(document, "$.fullname");String newEmail = JsonPath.read(document, "$.email");int age = JsonPath.read(document, "$.age");// 3. ตดตอฐานขอมลDatastoreService datastore = DatastoreServiceFactory.getDatastoreService();// 4. ระบเงอนไขการคนหาในทน คนตาม property ชอ email ซงมคาเทากบคาท รบจาก URLFilter filter = new FilterPredicate("email", FilterOperator.EQUAL, email);// 5. ระบช อ Entity ทตองการ queryQuery query = new Query("Customer").setFilter(filter);// 6. เร มดงขอมลจากฐานขอมลPreparedQuery pq = datastore.prepare(query);// 7. นา Entity ทคนหาเจอออกมาEntity customer = pq.asSingleEntity();// 8. นาคาใหมท สงมาเกบลง Entitycustomer.setProperty("fullname", fullname);customer.setProperty("email", newEmail);customer.setProperty("age", age);// 9. เกบ Entity ใหมdatastore.put(customer);return "success";

}

รบขอมลลกคาใน

รปแบบ JSON และรบ

อเมลเดมทาง URL

25

Page 26: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

ทดสอบการทางาน

26

Page 27: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

Service ลบขอมลลกคา

ไฟล Customer.java

รบอเมลลกคาทตองการ

ลบทาง URL@DELETE@Path("/{email}")public String delete(@PathParam("email") String email) {

System.out.println("Delete : " + email);// 1. ตดตอฐานขอมลDatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

// 2. ระบเง อนไขการคนหาในทน คนตาม property ชอ email ซงมคาเทากบคาท รบจาก URLFilter filter = new FilterPredicate("email", FilterOperator.EQUAL, email);

// 3. ระบชอ Entity ทตองการ queryQuery query = new Query("Customer").setFilter(filter);

// 4. เร มดงขอมลจากฐานขอมลPreparedQuery pq = datastore.prepare(query);

// 5. นา Entity ทคนหาเจอออกมาEntity customer = pq.asSingleEntity();

// 6. เร มลบขอมลลกคาdatastore.delete(customer.getKey());return "success";

}

27

Page 28: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

ทดสอบการทางาน

28

Page 29: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

กจกรรม

จงสรางเวบเซอรวสบน Google App Engine โดยใหบรการเกยวกบสนคา ซงม Service ดงน

Service ใหบรการ HTTP

Method

URL ในการเรยกเวบเซอรวส

รปแบบตามทกาหนดใน @Path

ขอมลเขา

@Consumes

ขอมลสงกลบ

@Produces

เกบขอมลสนคา POST http://appspot.com/rest/product ขอมลสนคา

รปแบบ JSON

รหสสนคา

รปแบบ Text

ใหรายการขอมลสนคา GET http://appspot.com/rest/product - ขอมลสนคา

รปแบบ JSON

ใหขอมลสนคาตามรหส GET http://appspot.com/rest/product/{id} - ขอมลสนคา

รปแบบ JSON

แกไขขอมลสนคา PUT http://appspot.com/rest/product/{id} ขอมลสนคา

รปแบบ JSON

ขอความ "success"

รปแบบ Text

ลบขอมลสนคา DELETE http://appspot.com/rest/product/{id} - ขอความ "success"

รปแบบ Text

29

Page 30: REST Web Service Provider บน Google App Engine · Google App Engine (GAE) GAE คือ แพลตฟอร์มที่ใช้สําหรับการพัฒนาและติดตั้งเว็บแอป

กจกรรม

ขอมลสนคาประกอบดวย รหสสนคา, ชอสนคา, รายละเอยด และราคา โดยใหเพมขอมลดงน

หลงจากทดสอบการทางานบน localhost ถกตองแลว ให deploy ขน Google App Engine

pid pname pdetail price

1 Centrum วตามนรวมจาก A ถง Zinc 350

2 Caltrate บารงกระดก เสรมวตามนด 760

3 Ester-C วตามนซ 500 mg ไมกดกระเพาะ 500

4 Glucosamine บารงขอตอ ปองกนขอเสอม 120

30