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

Post on 05-Oct-2020

4 views 0 download

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

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

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

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

ปฏบตการท 10

REST Web Service Provider

บน Google App Engine

1

Google App Engine (GAE)

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

Data Center

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

ชนดหนงบน Cloud Computing

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

2

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

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

https://appengine.google.com/

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

3

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

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

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

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

4

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

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

ในชอง Work with ดงน

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

เลอก Google Plugin for Eclipse

5

การสราง 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

ไฟล 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

Hello Service

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

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

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

}

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

เรยกโดยวธ GET

output มรปแบบเปน XML

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

8

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

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

Engine

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

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

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

9

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

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

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

Application ID Path ทระบใน Annotation

10

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

11

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

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

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", "theerayut@kku.ac.th");customer.setProperty("age", "25");

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

สราง Entity

กาหนดคาลงไป

ในแตละ Property

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

ลงฐานขอมล

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

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

กจกรรม

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

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

Application

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

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

Method: POST

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

"fullname": "Peter","email": "peter@kku.ac.th","age": 32

}

15

กจกรรม

16

กจกรรม

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

17

กจกรรม

ทาการ Deploy ขน Google App Engine

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

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

Method: POST

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

"fullname": "Peter","email": "peter@kku.ac.th","age": 32

}

18

กจกรรม

19

กจกรรม

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

เลอกเมน Datastore Viewer

20

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

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

22

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

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

24

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

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

26

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

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

28

กจกรรม

จงสรางเวบเซอรวสบน 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

กจกรรม

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

หลงจากทดสอบการทางานบน 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