79
370: Programming Web Services eek 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Embed Size (px)

Citation preview

Page 1: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

SE 370: Programming Web Services

Week 11: JSON & RESTfulCopyright © Steven W. Johnson

February 1, 2013

Page 2: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

asdf

Today:

2

Page 3: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

REST uses JSON to transmit data

JSON:

3

Page 4: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JavaScript Object Notation

Douglas Crockford (2002)

JSON: text-based, open data interchange system

JSON:

4

Page 5: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Purpose:

data interchange (kavşak) format

serialize data for network transmission

passes objects as strings from server to client

Intention: a light-weight alternative to XML

Incorporated into ISO

Part of ECMAScript, RFC 4627

JSON:

5

Page 6: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

M. C. Escher: (1898 – 1972)

JSON:

6

Page 7: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Basic format of JSON:

all data held in square brackets

each record held in curly brackets

field is made up of:

field name in quotes

value written appropriate to its data type

name/value pairs delimited by commas

JSON:

7

[ {“name”: value, “name”: “value”}, {“name”: value, “name”: “value”} ]

Page 8: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON formatting options:

JSON:

8http://www.json.org/

Ajax format for JSON

employees = [{"firstName":"John", "lastName":"Doe", "age":"18"},{"firstName":"Peter", "lastName":"Jones", "age":"21"}];

Page 9: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

REST:

each record in curly brackets

all records in square brackets

JSON:

9http://www.json.org/

0 1 2 3

0 [0,0] [0,1] [0,2] [0,3]

1 [1,0] [1,1] [1,2] [1,3]

2 [2,0] [2,1] [2,2] [2,3]

array[2][1]; //9array[1][3]; //7

array = [{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}];

Page 10: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Each represents the same data; “records”

Effectively a database table

JSON:

10

students = [ {“name”:“Ali”, “age”:”20”, “bolum”:“MBBF” }, {“name”:“Bahar”, “age”:”21”, “bolum”:“ITF” }]; Students

Name Age Bolum Gender

Ali 20 MBBF Erkek

Bahar 21 ITF Hanim

students[1].age //21

Page 11: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

An array of arrays or a database with 2 tables

‘Company’ is DB, tables ‘employees’, ‘managers’

JSON:

11

var company = {“employees”: [ { "firstName":"John" , "lastName":"Doe" , "age":"18"}, { "firstName":"Anna" , "lastName":"Smith" , "age":"20"}, { "firstName":"Peter" , "lastName":"Jones" , "age":"21"} ], “managers”: [ { "firstName":"Alice" , "lastName":“Williams" , "age":"19"}, { "firstName":"Carla" , "lastName":“Walker" , "age":"23"}, { "firstName":"Joe" , "lastName":“Evans" , "age":"22"} ]}

document.write(company.employees[2].firstName); //Peterdocument.write(company.managers[1].age); //23

Page 12: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Update table values also

JSON:

12

var company = {“employees”: [ { "firstName":"John" , "lastName":"Doe" , "age":"18"}, { "firstName":"Anna" , "lastName":"Smith" , "age":"20"}, { "firstName":"Peter" , "lastName":"Jones" , "age":"21"} ], “managers”: [ { "firstName":“Alice" , "lastName":“Williams" , "age":"19"}, { "firstName":“Carla" , "lastName":“Walker" , "age":"23"}, { "firstName":“Joe" , "lastName":“Evans" , "age":"22"} ]}

company.employees[2].firstName= “Steve”;

Page 13: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

x-dimensional array saved as ‘employees’

objname[i]fieldname:

JSON:

13

employees = [{ "firstName":"John" , "lastName":"Doe" , "age":18},{ "firstName":"Anna" , "lastName":"Smith" , "age":20},{ "firstName":"Peter" , "lastName":"Jones" , "age":21}];

document.write(employees[1].age); //20

Page 14: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON supports UTF-8; Notepad doesn’t

Must save UTF-8 files using UTF-8, not ANSI

JSON:

14

Page 15: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Derived from JavaScript to represent objects:

simple data structures

associative arrays

parsers available for many languages

JSON:

15

Page 16: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON versus XML:

JSON:

16

“students”= [ {“name”: “Ali”, “age”: “20”, “bolum”: “MBBF”}, {“name”: “Bahar”, “age”: “21”, “bolum”: “ITF”}]

<?xml version=“1.0” encoding=“utf-8”?>

<students> <student> <name>Ali</name> <age>20</age> <bolum>MBBF</bolum> <gender>Erkek</gender> </student> <student> <name>Bahar</name> <age>21</age> <bolum>ITF</bolum> <gender>Hanim</gender> </student> </students>

Page 17: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON:

MIME type: application/json

Extension: .json

Basic form: uses name/value pairs

JSON can be stored in either .json and .txt

17

{“firstname”: “Canan”}

firstname = “Canan”;

¸

data.txtdata.json

Page 18: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON is JavaScript

‘XML’ with anorexia; faster to parse*

Easier to write, lightweight, less verbose

True data format, not a meta language

JSON advantages:

18

<name>Steve</name>

“name”: “Steve”,

Page 19: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

True for both XML and JSON:

language independent

cross-platform

self-describing

human readable

hierarchical (data in data, values describing values)

JSON similarities:

19

Page 20: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON is JavaScript

Requires use of ‘eval’ function*

Reserved JavaScript keywords can’t be used as

element names

XML is more familiar, more like HTML

XML more precise due to specific DTD

XML has better support

JSON disadvantages:

20

Page 21: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Data types of JSON: (just like JavaScript)

number (JavaScript has no ‘float’ and

‘integer’)

string

Boolean

array

object

null

JSON:

21

Page 22: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Both describe objects as strings

Both are suitable for use in web services

Both enjoy broad support, libraries, etc

Tools exist to convert

Data conversion can easily be done manually

JSON versus XML:

22

<name>Steve</name>

“name”: “Steve”,

Page 23: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON:

23

<name>Ali</name>

a=indexOf("<") d=lastIndexOf("<")

b=indexOf(">") c=lastIndexOf("<")

"name"=substr(a+1,b-1)

"Ali"=substring(b+1,c)

"</"+name+">"

Convert XML to JSON:

String.substr (start, # of characters);String.substring(start, finish);

Page 24: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON:

24

<name>Ali</name>

a=indexOf("<") d=lastIndexOf("<")

b=indexOf(">") c=lastIndexOf("<")

"name"=substr(a+1,b-1)

"Ali"=substring(b+1,c)

document.write(name, value);

Convert XML to JSON:

Page 25: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

JSON:

25

<firstname>Ali</firstname><lastname>Zeybek</lastname><age>23</age>for (i=0; i<length; i++) { name = String.substr(a+1,b-1) value = String.substring(b+1,c) document.write(name, value);}

Convert XML to JSON:

for (i=0; i<length; i++) { name = String[i].substr(a+1,b-1) value = String[i].substring(b+1,c) document.write("\""+name+"\": \""+value+"\", ");}

Page 26: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Lab: JSON Parser

26

<CustomerNumber>7458</CustomerNumber><FirstName>Elif</FirstName><Surname>Bardukoglu</Surname><Discount>5</Discount>

Convert XML to name/value pairs

Output: “CustomerNumber”: “3568”,

Assume data held in single string ‘data’

<Cust>123</Cust> <First>Ali</First>

Data= “<CustomerNumber>7458</CustomerNumber> <FirstName>Elif</FirstName> <Surname>Bardukoglu</Surname> <Discount>5</Discount>”;

Page 27: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Lab: JSON Parser

27

<script>data = "<CustomerNumber>7458</CustomerNumber> <FirstName>Elif</FirstName> <Surname>Bardukoglu</Surname> <Discount>5</Discount>";

var rawdata = data.split(" ");count = rawdata.length;for (i=0; i<count; i++) { namestart = rawdata[i].indexOf("<");

nameend = rawdata[i].indexOf(">");nametext = rawdata[i].substring(namestart + 1, nameend);valuestart = nameend;valueend = rawdata[i].lastIndexOf("<");valuetext = rawdata[i].substring(valuestart + 1, valueend);document.write("\""+nametext+"\": \""+valuetext+"\"");if (i < count-1) document.write(", ");

}</script>

Page 28: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Break 28

Page 29: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Lab: JSON Parser

29

Convert JSON to XML:

<students> <student> <name>Ali</name> <age>20</age> <bolum>MBBF</bolum> </student> <student> <name>Bahar</name> <age>21</age> <bolum>ITF</bolum> </student></students>

student=[ {"name":"Ali","age":"20","bolum":"MBBF"}, {"name":"Bahar","age":"21","bolum":"ITF"}];

Page 30: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Lab: JSON Parser

30

student=[ {"name":"Ali","age":"20","bolum":"MBBF"}, {"name":"Bahar","age":"21","bolum":"ITF"}];

Convert JSON to XML:

Root element is ‘students’ (not considered)

‘Students’ is root element

Text before “=” is like record name

Rows are like records; holds field names, values

Page 31: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Lab: JSON Parser

31

Convert JSON to XML: (format is different)

Assume no spaces in the ‘data’

<students> <student> <name>Ali</name> <age>20</age> <bolum>MBBF</bolum> </student> <student> <name>Bahar</name> <age>21</age> <bolum>ITF</bolum> </student></students>

student=[{"name":"Ali","age":"20","bolum":"MBBF"},{"name":"Bahar","age":"21","bolum":"ITF"}];

Page 32: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Lab: JSON Parser

32

Convert JSON to XML: (format is different)

student=[{"name":"Ali","age":"20","bolum":"MBBF"},{"name":"Bahar","age":"21","bolum":"ITF"}];

indexOf(“\””)

lastIndexOf(“\””)

replace(“\”,\””, “ “)

replace(“\“:\””, “ “)

name Ali age 20 bolum MBBF"},{"name Bahar age 21 bolum ITF

split(“ ”)

Page 33: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Quiz:

1. What is the address of the value 24?

33

12 16 175 8 123 24 6

[2, 1]

Page 34: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Quiz:

2. Place this array in the table:

34

1 2 34 5 67 8 9

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Page 35: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Quiz:

3. What XML tag is most like a record?

35

<bbb>

<aaa> <bbb> <ccc>data</ccc> <ccc>data</ccc> <ccc>data</ccc> </bbb></aaa>

Page 36: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Quiz:

4. From <aaa>, the address of <bbb>?

5. Child nodes of <ccc>

6. What is the root node?

36

<aaa>[0].childNodes[0]

<aaa> <bbb> <ccc>11</ccc> <ccc>22</ccc> <ccc>33</ccc> </bbb></aaa>

0

<aaa>

Page 37: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Quiz:

7. Children of <aaa>?

8. Value of <bbb>[0].childNodes[1].childNodes[0].nodeValue

37

1

<aaa> <bbb> <ccc>11</ccc> <ccc>22</ccc> <ccc>33</ccc> </bbb></aaa>

22

Page 38: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Quiz:

9. JSON array of 3 names:

38

friends = [ {“name”:”Ali”, “name”:”Bahar”, “name”:”Canan”}];

Page 39: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Quiz:

10. What is the value of company.employees[1].age?

39

var company = {“employees”: [ { "firstName":"John" , "lastName":"Doe" , "age":18}, { "firstName":"Anna" , "lastName":"Smith" , "age":20}, { "firstName":"Peter" , "lastName":"Jones" , "age":21} ], “managers”: [ { "firstName":“Alice" , "lastName":“Williams" , "age":19}, { "firstName":“Carla" , "lastName":“Walker" , "age":23}, { "firstName":“Joe" , "lastName":“Evans" , "age":22} ]}

20

Page 40: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

SE 370: Programming Web Services

Week 12: JSONCopyright © Steven W. Johnson

February 1, 2013

Page 41: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

asd

asd

asd

Today:

41

Page 42: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Labs:

Assignment:

Week 2:

42

Page 43: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

asd

Course textbook:

43

Page 44: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Break 44

Page 45: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Labs:

Assignment:

Week 2:

45

Page 46: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Deploy (rt. Click on project – Deploy)

http://localhost:8080/HelloTest/webresources/generic

Lab: Hello RESTful Webapps

46

Page 47: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

REST:

GET/POST/PUT/DELETE methods of HTTP

Requires tomcat

Made in maven

RESTEasy

NetBeans

https://netbeans.org/kb/docs/websvc/intro-ws.html

http://yiyujia.blogspot.com/2011/09/simple-tutorial-about-creating-jersey.html***

47

Page 48: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Customers in table:

Lab: Web Service using REST

48

@Stateless@Path("customerdb.customer")public class CustomerFacadeREST extends AbstractFacade<Customer> { @PersistenceContext(unitName = "CustomerDBPU") private EntityManager em;

public CustomerFacadeREST() { super(Customer.class); }

@POST @Override @Consumes({"application/xml", "application/json"}) public void create(Customer entity) { super.create(entity); }

@PUT @Override @Consumes({"application/xml", "application/json"}) public void edit(Customer entity) { super.edit(entity); }

@DELETE @Path("{id}") public void remove(@PathParam("id") Integer id) { super.remove(super.find(id)); }

@GET @Path("{id}") @Produces({"application/xml", "application/json"}) public Customer find(@PathParam("id") Integer id) { return super.find(id); }

@GET @Override @Produces({"application/xml", "application/json"}) public List<Customer> findAll() { return super.findAll(); }

@GET @Path("{from}/{to}") @Produces({"application/xml", "application/json"}) public List<Customer> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) { return super.findRange(new int[]{from, to}); }

@GET @Path("count") @Produces("text/plain") public String countREST() { return String.valueOf(super.count()); }

@Override protected EntityManager getEntityManager() { return em; } }

Page 49: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Customers in table:

Lab: Web Service using REST

49

@Stateless@Path("customerdb.customer")public class CustomerFacadeREST extends AbstractFacade<Customer> { @PersistenceContext(unitName = "CustomerDBPU") private EntityManager em;

public CustomerFacadeREST() { super(Customer.class); }

@POST @Override @Consumes({"application/xml", "application/json"}) public void create(Customer entity) { super.create(entity); }

@PUT @Override @Consumes({"application/xml", "application/json"}) public void edit(Customer entity) { super.edit(entity); }

@DELETE @Path("{id}") public void remove(@PathParam("id") Integer id) { super.remove(super.find(id)); }

@GET @Path("{id}") @Produces({"application/xml", "application/json"}) public Customer find(@PathParam("id") Integer id) { return super.find(id); }

@GET @Override @Produces({"application/xml", "application/json"}) public List<Customer> findAll() { return super.findAll(); }

@GET @Path("{from}/{to}") @Produces({"application/xml", "application/json"}) public List<Customer> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) { return super.findRange(new int[]{from, to}); }

@GET @Path("count") @Produces("text/plain") public String countREST() { return String.valueOf(super.count()); }

@Override protected EntityManager getEntityManager() { return em; } }

Page 50: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Create record 14 as yourself

Lab: Web Service using REST

50

<?xml version="1.0" encoding="UTF-8"?> <customers> <customer> <addressline1>111 E. Las Olivas Blvd</addressline1> <addressline2>Suite 51</addressline2> <city>Fort Lauderdale</city> <creditLimit>100000</creditLimit> <customerId>1</customerId> <discountCode> <discountCode>78</discountCode> <rate>0.00</rate> </discountCode> <email>[email protected]</email> <fax>305-555-0189</fax> <name>Jumbo Eagle Corp</name> <phone>305-555-0188</phone> <state>FL</state> </customer>

<?xml version="1.0" encoding="UTF-8"?> <customers> <customer> <addressline1>6482 Sokak #33/1</addressline1> <addressline2>Suite 51</addressline2> <city>Fort Lauderdale</city> <creditLimit>100000</creditLimit> <customerId>14</customerId> <discountCode> <discountCode>75</discountCode> <rate>0.00</rate> </discountCode> <email>[email protected]</email> <fax>305-555-0189</fax> <name>Jumbo Eagle Corp</name> <phone>305-555-0188</phone> <state>FL</state> </customer>

{"discountCode":"H","rate":16.00,"customerCollection":[{"customerId":36,"name":"Bob Hosting Corp.","addressline1":"65653 Lake Road","addressline2":"Suite 2323","city":"San Mateo","state":"CA","phone":"650-555-0160","fax":"650-555-0161","email":"www.bobhostcorp.example.com","creditLimit":65000,"discountCode":

Page 51: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Create the client application:

File – New Project

Lab: Web Service using REST

51

Page 52: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Uncheck ‘Create Main Class’ – Finish

Lab: Web Service using REST

52

Page 53: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Rt. Click Project – New – Entity Classes from Database

Lab: Web Service using REST

53

Page 54: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Add package name - Finish

Lab: Web Service using REST

54

Page 55: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Rt. Click on client project

New – Other… - Web Services – RESTful Java Client

Lab: Web Service using REST

55

Page 56: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Create the client application:

File – New Project

Lab: Web Service using REST

56

Page 57: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

SE 370: Programming Web Services

Week 12: JSONCopyright © Steven W. Johnson

February 1, 2013

Page 58: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Use CRON to run the client page

Asadmin in glassfish-3.1.2.2 – bin

http://www.javascool.com/2010/04/15/ejb-timer-service/

Lab: CRONentry

58

rs.next();int id=921;String customernumber = "ABCD";String sku = rs.getString("sku");int qtyordered = rs.getInt("maxstock") - rs.getInt("onhand");

Page 59: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Open ‘Projects’ tab – File – New Project - Java

Lab: CRONentry

59

Page 60: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Name: OrderWS (all server side is ‘order’)

Main Class: DBConnect

Lab: CRONentry

60

Page 61: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Lab: CRONentry

61

Gives a class DBConnect/* * To change this template, choose Tools | Templates * and open the template in the editor. */package orderws;

/** * * @author Steve */public class DBConnect {

/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here }}

Page 62: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Lab: CRONentry

62

Connection to DB requires imports:

unused import (will be used later)

package orderws;

import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;

public class DBConnect {

/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here }}

Page 63: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Set up the connection string:

Error: ‘unreported exception’ (add try/catch: coming)

Lab: CRONentry

63

package orderws;

import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;

public class DBConnect {

/** * @param args the command line arguments */ public static void main(String[] args) { String host = “jdbc:derby://localhost:1527/order”; String user = “stevej” String pass = “izmir” Connection con = DriverManager.getConnection(host, user, pass); }}

Page 64: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Add try/catch framework:

Lab: CRONentry

64

public static void main(String[] args) { try { String host = “jdbc:derby://localhost:1527/order”; String user = “stevej” String pass = “izmir” Connection con = DriverManager.getConnection(host, user, pass); } catch (SQLException err) { System.out.println(err.getMessage());}

Page 65: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Add two imports to top

These are used to create the query and recordset

Lab: CRONentry

65

import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;//import java.sql.ResultSet;

Page 66: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Create INSERT query

As written, works on single INSERT only

Dummy data added to test format

Lab: CRONentry

66

Statement stmt = con.createStatement();

int id = 12;String customernumber = "Server";String sku = "428575 87412";int qtyordered = 9;

String SQL = "INSERT INTO orderentry VALUES ("+id+", '"+customernumber+"', '"+sku+"', "+qtyordered+")";stmt.executeUpdate(SQL);System.out.println("Successful Execution");

Page 68: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Jersey

Restlet

JBoss RESTEasy

Apache CXF

Triaxrs

Apache Wink

eXo

Implementations of REST

68

Page 69: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Create the client application:

File – New Project

Lab: Web Service using REST

69

Page 70: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Create the client application:

File – New Project

Lab: Web Service using REST

70

Page 71: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Create the client application:

File – New Project

Lab: Web Service using REST

71

Page 72: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

Create the client application:

File – New Project

Lab: Web Service using REST

72

Page 73: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

REST:

REST architecture describes six constraints:

client/server model; separation of concerns

Stateless: no memory of prior communications

Cacheable

Layered system: may use intermediary servers

Code on demand (optional)

Uniform interface (wikipedia)

https://en.wikipedia.org/wiki/Representational_state_transfer73

Page 74: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

DiscountCode.java

Lab: Client side of REST

74

Page 75: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

DiscountCode.java

Lab: Client side of REST

75

Page 76: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

http://www.oracle.com/technetwork/articles/javase/index-137171.html

http://www.myeclipseide.com/documentation/quickstarts/webservices_rest/

http://rest.elkstein.org/

http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html

http://www.packtpub.com/restful-php-web-services/book

http://davidwalsh.name/web-service-php-mysql-xml-json

http://www.ibm.com/developerworks/opensource/tutorials/os-php-webservice/

http://publib.boulder.ibm.com/infocenter/wmbhelp/v7r0m0/index.jsp?topic=%2Fcom.ibm.etools.mft.samples.jsonrest.doc%2Fdoc%2Fintroduction.htm

Today:

76

Page 78: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

http://www.javaworld.com/javaworld/jw-01-2013/130124-web-services-are-dead-long-live-rest.htmlasd

http://www.ibm.com/developerworks/web/library/wa-aj-tomcat/

http://drupal.org/node/1860564

http://code.google.com/p/staff/wiki/ExampleCalculatorService

https://access.redhat.com/site/documentation/en-US/JBoss_Developer_Studio/4.0/html/JBoss_Web_Services_User_Guide/sample_web_service_wizards-sample_restful_web_service

.html

RESTEasy installed

http://community.jaspersoft.com/wiki/getting-started-rest-web-service-api**

http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html PHP

http://markroland.com/blog/restful-php-api/

http://phpmaster.com/writing-a-restful-web-service-with-slim/

http://www.xfront.com/REST-Web-Services.html

Today:

78

Page 79: SE 370: Programming Web Services Week 11: JSON & RESTful Copyright © Steven W. Johnson February 1, 2013

asd

asd

asd

Today:

79