You are on page 1of 26

JavaFX Rich Internet Applications with RESTful web services, Jersey API and JSON

YaJuG, July 6th

Sébastien Stormacq

Senior Software Architect, Sun Microsystems Belgium & Luxembourg

Learn how to architect and build JavaFX RIA application with asynchronous communication to REST and JSON based web services.

- or -

How to place maximum numbers of buzz words and acronyms in a presentation title.

2009 CommunityOne Conference: North

|

no.sun.com/communityone

2

Agenda

Architecture OverviewAgenda JavaFX in a nutshell REST based Web Service JSON Put it all Together 2009 CommunityOne

JavaFX in a nutshellAgenda Architecture Overview REST based Web Service JSON Put it all Together 2009 CommunityOne Conference: North

REST based Web ServiceAgenda Architecture Overview JavaFX in a nutshell JSON Put it all Together 2009 CommunityOne Conference: North

JSONOverview JavaFX in a nutshell REST based Web Service Put it all Together 2009 CommunityOne Conference:

Put it all TogetherOverview JavaFX in a nutshell REST based Web Service JSON 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

3

Agenda

Architecture OverviewAgenda JavaFX in a nutshell REST based Web Service JSON Put it all Together 2009 CommunityOne

JavaFX in a nutshellAgenda Architecture Overview REST based Web Service JSON Put it all Together 2009 CommunityOne Conference: North

REST based Web ServiceAgenda Architecture Overview JavaFX in a nutshell JSON Put it all Together 2009 CommunityOne Conference: North

JSONOverview JavaFX in a nutshell REST based Web Service Put it all Together 2009 CommunityOne Conference:

Put it all TogetherOverview JavaFX in a nutshell REST based Web Service JSON 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

4

Architecture Overview

RIA Leveraging existing backend services

JavaFX Client RIA
JavaFX
Client RIA
RIA Leveraging existing backend services JavaFX Client RIA http / json REST Web Service GlassFish v3

http / json

existing backend services JavaFX Client RIA http / json REST Web Service GlassFish v3 2009 CommunityOne

REST Web Service

GlassFish v3

2009 CommunityOne Conference: North

|

no.sun.com/communityone

RIA http / json REST Web Service GlassFish v3 2009 CommunityOne Conference: North | no.sun.com/communityone jdbc
RIA http / json REST Web Service GlassFish v3 2009 CommunityOne Conference: North | no.sun.com/communityone jdbc

jdbc

5

Agenda

Architecture OverviewAgenda JavaFX in a nutshell REST based Web Service JSON Put it all Together 2009 CommunityOne

JavaFX in a nutshellAgenda Architecture Overview REST based Web Service JSON Put it all Together 2009 CommunityOne Conference: North

REST based Web ServiceAgenda Architecture Overview JavaFX in a nutshell JSON Put it all Together 2009 CommunityOne Conference: North

JSONOverview JavaFX in a nutshell REST based Web Service Put it all Together 2009 CommunityOne Conference:

Put it all TogetherOverview JavaFX in a nutshell REST based Web Service JSON 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

6

JavaFX in a nutshell

Scripting Language & API for Graphical ApplicationsJavaFX in a nutshell • Cool Language w/ data binding and triggers • Rich Graphics API

Cool Language w/ data binding and triggers

Rich Graphics API

Multimedia ready

Tools for developers and designersand triggers • Rich Graphics API • Multimedia ready • JavaFX SDK • NetBeans • Sample

JavaFX SDK

NetBeans

Sample

Adobe Illustrator & Photoshop plugins

• Sample • Adobe Illustrator & Photoshop plugins Built on top of Java™ platform 2009 CommunityOne

Built on top of Java™ platform• Sample • Adobe Illustrator & Photoshop plugins 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

7

JavaFX Code Sample

Declarative, Object OrientedJavaFX Code Sample Stage, Scene Stage { title: "Application title" width: 250 height: 270 scene: Scene

Stage, SceneJavaFX Code Sample Declarative, Object Oriented Stage { title: "Application title" width: 250 height: 270

Stage { title: "Application title" width: 250 height: 270

scene: Scene { content: Text { value: "Hello JavaFX World"

}

}

}

2009 CommunityOne Conference: North

|

no.sun.com/communityone

8

Agenda

Architecture OverviewAgenda JavaFX in a nutshell REST based Web Services JSON Put it all Together 2009 CommunityOne

JavaFX in a nutshellAgenda Architecture Overview REST based Web Services JSON Put it all Together 2009 CommunityOne Conference: North

REST based Web ServicesAgenda Architecture Overview JavaFX in a nutshell JSON Put it all Together 2009 CommunityOne Conference: North

JSONOverview JavaFX in a nutshell REST based Web Services Put it all Together 2009 CommunityOne Conference:

Put it all TogetherOverview JavaFX in a nutshell REST based Web Services JSON 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

9

RESTful web services

REST Architecture PrinciplesRESTful web services • Re presentational S tate T ransfer • Everything is a resource •

Representational S tate T ransfer

Everything is a resource

Resources are addressable

Resources have an interface (operations and data types)

Protocol is client-server, stateless, cacheable, layered

Applied to web servicesProtocol is client-server, stateless, cacheable, layered • Web Service is accessible through an URI •

Web Service is accessible through an URI

Operations are HTTP primitives (PUT, GET, DELETE, …)

Web Service returns a MIME Type (XML, JSON, YAML,

)

More resource efficient than SOAP• Web Service returns a MIME Type (XML, JSON, YAML, ) 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

10

RESTful web services : a Java API

JSR 311, aka aka JAX-RSRESTful web services : a Java API Jersey is JAX-RS Reference Implementation RESTful web service is

Jersey is JAX-RS Reference ImplementationRESTful web services : a Java API JSR 311, aka aka JAX-RS RESTful web service is

RESTful web service isaka aka JAX-RS Jersey is JAX-RS Reference Implementation • A Java class • A set of

A Java class

A set of methods

Use Java annotations to representweb service is • A Java class • A set of methods • The resources (the

The resources (the URI)

The Operations

The Data Types (as MIME types)

At runtime, Jersey dispatches calls to codeURI) • The Operations • The Data Types (as MIME types) Will be part of upcoming

Will be part of upcoming Java EE 6 specification(as MIME types) At runtime, Jersey dispatches calls to code 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

11

RESTful web services : an example

// The Java class will be hosted at // the URI path "/helloworld" @Path("/helloworld") public class HelloWorldResource {

// The Java method will process // HTTP GET requests @GET // The Java method will produce content identified // by the MIME Media type "text/plain" @Produces("text/plain") public String getClichedMessage() { // Return some cliched textual content return "Hello World";

}

}

2009 CommunityOne Conference: North

|

no.sun.com/communityone

12

Agenda

Architecture OverviewAgenda JavaFX in a nutshell REST based Web Services JSON Put it all Together 2009 CommunityOne

JavaFX in a nutshellAgenda Architecture Overview REST based Web Services JSON Put it all Together 2009 CommunityOne Conference: North

REST based Web ServicesAgenda Architecture Overview JavaFX in a nutshell JSON Put it all Together 2009 CommunityOne Conference: North

JSONOverview JavaFX in a nutshell REST based Web Services Put it all Together 2009 CommunityOne Conference:

Put it all TogetherOverview JavaFX in a nutshell REST based Web Services JSON 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

13

JSON, JavaScript Object Notation

A data format to exchange data structuresJSON, JavaScript Object Notation Language independent Mainly used for object serialization and AJAX { "firstName"

Language independentObject Notation A data format to exchange data structures Mainly used for object serialization and AJAX

Mainly used for object serialization and AJAXdata format to exchange data structures Language independent { "firstName" : "John" ,

{

"firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021

}, "phoneNumbers": [ "212 555-1234", "646 555-4567"

]

}

2009 CommunityOne Conference: North

|

no.sun.com/communityone

14

JSON vs XML

JSON is more CPU, memory & network efficient*JSON vs XML • XML • JSON $ jar tfv classes.jar | grep xml | wc

XML

JSON

$ jar tfv classes.jar | grep xml | wc -l

3563

$ ls -al json/*.java | wc -l

7

XML has much more to offer in terms of semantics| grep xml | wc -l 3563 $ ls -al json/*.java | wc -l 7 XML

XML Ubiquity| wc -l 7 XML has much more to offer in terms of semantics It all

It all depends to your project size, team and … the architect's decisionhas much more to offer in terms of semantics XML Ubiquity * Comparison is based on

* Comparison is based on Java SE 1.6 and is totally not objective. XML classes include full support for schema, cryptography, web services and much more other functionalities not offered by JSON parsers

2009 CommunityOne Conference: North

|

no.sun.com/communityone

15

Agenda

Architecture OverviewAgenda JavaFX in a nutshell REST based Web Services JSON Put it all together 2009 CommunityOne

JavaFX in a nutshellAgenda Architecture Overview REST based Web Services JSON Put it all together 2009 CommunityOne Conference: North

REST based Web ServicesAgenda Architecture Overview JavaFX in a nutshell JSON Put it all together 2009 CommunityOne Conference: North

JSONOverview JavaFX in a nutshell REST based Web Services Put it all together 2009 CommunityOne Conference:

Put it all togetherOverview JavaFX in a nutshell REST based Web Services JSON 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

16

Putting it all Together

The Big Picture

Putting it all Together The Big Picture http / json REST Web Service GlassFish v3 2009
Putting it all Together The Big Picture http / json REST Web Service GlassFish v3 2009

http / json

Putting it all Together The Big Picture http / json REST Web Service GlassFish v3 2009

REST Web Service

GlassFish v3

2009 CommunityOne Conference: North

|

no.sun.com/communityone

17

Putting it all Together

JSON Data Structure

Using json.org provided API (7 classes)Putting it all Together JSON Data Structure Return an array with 4 random integer { "

Return an array with 4 random integerJSON Data Structure Using json.org provided API (7 classes) { " Values " : [ 5,99,42,20

{ "Values" : [ 5,99,42,20 ]

}

4 random integer { " Values " : [ 5,99,42,20 ] } result = new JSONStringer()

result = new JSONStringer() .object() .key("Values") .array()

.value(rand.nextInt(100))

.value(rand.nextInt(100))

.value(rand.nextInt(100))

.value(rand.nextInt(100))

.endArray()

.endObject().toString();

2009 CommunityOne Conference: North

|

no.sun.com/communityone

18

Putting it all Together

A RESTful web service with Jersey

@Path("values") public class ValuesResource {

@GET @Produces("application/json") public String getValues () { String result;

try { result = } catch (JSONException e) {

}

}

}

return result;

2009 CommunityOne Conference: North

|

no.sun.com/communityone

19

Putting it all Together

A JavaFX Application

Stage { title: "Application title" width: 250 height: 270

scene: Scene { content: PieChart {

}

}

data: [

}

]

270 scene: Scene { content: PieChart { } } data: [ } ] Reusing JavaFX 1.2

Reusing JavaFX 1.2 PieChart component270 scene: Scene { content: PieChart { } } data: [ } ] Alternative : Open

Alternative : Open Source PieChartFX + online tutorial to build it from scratch{ } } data: [ } ] Reusing JavaFX 1.2 PieChart component • http://blogs.sun.com/sebsto 2009 CommunityOne

http://blogs.sun.com/sebsto

2009 CommunityOne Conference: North

|

no.sun.com/communityone

20

Putting it all Together

An Asynchronous Communication

var request : HttpRequest = HttpRequest {

location: "

";

onInput: function(is: InputStream) {

//parsing code

}

}

HttpRequest will connect and retrieve content will connect and retrieve content

onInput used to trigger parsing code used to trigger parsing code

2009 CommunityOne Conference: North

|

no.sun.com/communityone

21

Putting it all Together

JSON Parser : JavaFX meets Java

var data : JSONArray = new JSONObject(value).getJSONArray("Values");

for (i in [0

data.length()

- 1]) {

insert data.getDouble(i) into arcValues;

}

JavaFX uses the very same JSON parser as Java- 1]) { insert data.getDouble(i) into arcValues ; } Notice special JavaFX constructs • for loop

Notice special JavaFX constructsarcValues ; } JavaFX uses the very same JSON parser as Java • for loop •

for loop insert … into

2009 CommunityOne Conference: North

|

no.sun.com/communityone

22

Putting it all Together

A JavaFX Polling Mechanism (JavaFX meets Java, again)

class PieChartTask extends TimerTask { override function run() { //wrap existing connection and parsing code

};

}

def timer : Timer = new Timer("TimerThread"); def task : PieChartTask = new PieChartTask(); timer.schedule(task, 0, 5000);

JavaFX use existing java.util.Timer classes java.util.Timer classes

JavaFX application polls web service every 5000ms0, 5000); JavaFX use existing java.util.Timer classes 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

23

Demo

It is not just slideware … it really works !

2009 CommunityOne Conference: North

|

no.sun.com/communityone

24

Summary

JavaFXSummary • Powerful language, API and tools to build RIA • Based on top and leverage

Powerful language, API and tools to build RIA

Based on top and leverage Java™ platform

RESTful Web Servicesto build RIA • Based on top and leverage Java™ platform • Lightweight web services, might

Lightweight web services, might suite many applications

Easy to use Java API

JSONmight suite many applications • Easy to use Java API • Lightweight data format • Easy

Lightweight data format

Easy to create and parse

Combining all of them is straightforward• Lightweight data format • Easy to create and parse Use whatever technology is appropriate in

Use whatever technology is appropriate in your project / company context !to create and parse Combining all of them is straightforward 2009 CommunityOne Conference: North |

2009 CommunityOne Conference: North

|

no.sun.com/communityone

25

JavaFX Rich Internet Applications with RESTful web services, Jersey API and JSON

YaJuG, July 6th

Sébastien Stormacq