javafx rest

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

Upload: harish

Post on 18-Nov-2014

851 views

Category:

Documents


1 download

DESCRIPTION

Implementation of RESTful webservices using JavaFX

TRANSCRIPT

Page 1: JavaFX Rest

JavaFX Rich Internet Applicationswith RESTful web services, Jersey API and JSON

YaJuG, July 6th

Sébastien StormacqSenior Software Architect, Sun Microsystems Belgium & Luxembourg

Page 2: JavaFX Rest

22009 CommunityOne Conference: North | no.sun.com/communityone

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.

Page 3: JavaFX Rest

32009 CommunityOne Conference: North | no.sun.com/communityone

Agenda

Architecture OverviewJavaFX in a nutshellREST based Web ServiceJSONPut it all Together

Page 4: JavaFX Rest

42009 CommunityOne Conference: North | no.sun.com/communityone

Agenda

Architecture OverviewJavaFX in a nutshellREST based Web ServiceJSONPut it all Together

Page 5: JavaFX Rest

52009 CommunityOne Conference: North | no.sun.com/communityone

Architecture OverviewRIA Leveraging existing backend services

GlassFish v3

JavaFXClient RIA

RESTWeb Service

jdbchttp / json

Page 6: JavaFX Rest

62009 CommunityOne Conference: North | no.sun.com/communityone

Agenda

Architecture OverviewJavaFX in a nutshellREST based Web ServiceJSONPut it all Together

Page 7: JavaFX Rest

72009 CommunityOne Conference: North | no.sun.com/communityone

JavaFX in a nutshell

Scripting Language & API for Graphical Applications• Cool Language w/ data binding and triggers• Rich Graphics API• Multimedia ready

Tools for developers and designers• JavaFX SDK• NetBeans• Sample• Adobe Illustrator & Photoshop plugins

Built on top of Java™ platform

Page 8: JavaFX Rest

82009 CommunityOne Conference: North | no.sun.com/communityone

JavaFX Code Sample

Declarative, Object OrientedStage, Scene

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

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

Page 9: JavaFX Rest

92009 CommunityOne Conference: North | no.sun.com/communityone

Agenda

Architecture OverviewJavaFX in a nutshellREST based Web ServicesJSONPut it all Together

Page 10: JavaFX Rest

102009 CommunityOne Conference: North | no.sun.com/communityone

RESTful web services

REST Architecture Principles• Representational State Transfer• 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 services• 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

Page 11: JavaFX Rest

112009 CommunityOne Conference: North | no.sun.com/communityone

RESTful web services : a Java API

JSR 311, aka aka JAX-RSJersey is JAX-RS Reference ImplementationRESTful web service is • A Java class• A set of methods

Use Java annotations to represent• The resources (the URI)• The Operations• The Data Types (as MIME types)

At runtime, Jersey dispatches calls to code Will be part of upcoming Java EE 6 specification

Page 12: JavaFX Rest

122009 CommunityOne Conference: North | no.sun.com/communityone

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"; }}

Page 13: JavaFX Rest

132009 CommunityOne Conference: North | no.sun.com/communityone

Agenda

Architecture OverviewJavaFX in a nutshellREST based Web ServicesJSONPut it all Together

Page 14: JavaFX Rest

142009 CommunityOne Conference: North | no.sun.com/communityone

JSON, JavaScript Object Notation

A data format to exchange data structuresLanguage independentMainly used for object serialization and AJAX

{ "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "phoneNumbers": [ "212 555-1234", "646 555-4567" ] }

Page 15: JavaFX Rest

152009 CommunityOne Conference: North | no.sun.com/communityone

JSON vs XML

JSON is more CPU, memory & network efficient*• XML

• JSON

XML has much more to offer in terms of semanticsXML Ubiquity

It all depends to your project size, team and …the architect's decision

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

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

* 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

Page 16: JavaFX Rest

162009 CommunityOne Conference: North | no.sun.com/communityone

Agenda

Architecture OverviewJavaFX in a nutshellREST based Web ServicesJSONPut it all together

Page 17: JavaFX Rest

172009 CommunityOne Conference: North | no.sun.com/communityone

Putting it all TogetherThe Big Picture

GlassFish v3

RESTWeb Service

http / json

Page 18: JavaFX Rest

182009 CommunityOne Conference: North | no.sun.com/communityone

Putting it all TogetherJSON Data Structure

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();

Using json.org provided API (7 classes)Return an array with 4 random integer

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

Page 19: JavaFX Rest

192009 CommunityOne Conference: North | no.sun.com/communityone

Putting it all TogetherA 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; }}

Page 20: JavaFX Rest

202009 CommunityOne Conference: North | no.sun.com/communityone

Putting it all TogetherA JavaFX Application

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

scene: Scene { content: PieChart { data: [ ... ] } }}Reusing JavaFX 1.2 PieChart componentAlternative : Open Source PieChartFX + online tutorial to build it from scratch• http://blogs.sun.com/sebsto

Page 21: JavaFX Rest

212009 CommunityOne Conference: North | no.sun.com/communityone

Putting it all TogetherAn Asynchronous Communication

var request : HttpRequest = HttpRequest { location: "...";

onInput: function(is: InputStream) {

//parsing code

}}

HttpRequest will connect and retrieve content

onInput used to trigger parsing code

Page 22: JavaFX Rest

222009 CommunityOne Conference: North | no.sun.com/communityone

Putting it all TogetherJSON 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 JavaNotice special JavaFX constructs• for loop• insert … into

Page 23: JavaFX Rest

232009 CommunityOne Conference: North | no.sun.com/communityone

Putting it all TogetherA 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 classesJavaFX application polls web service every 5000ms

Page 24: JavaFX Rest

242009 CommunityOne Conference: North | no.sun.com/communityone

DemoIt is not just slideware … it really works !

Page 25: JavaFX Rest

252009 CommunityOne Conference: North | no.sun.com/communityone

Summary

JavaFX• Powerful language, API and tools to build RIA• Based on top and leverage Java™ platform

RESTful Web Services• Lightweight web services, might suite many applications• Easy to use Java API

JSON• Lightweight data format• Easy to create and parse

Combining all of them is straightforwardUse whatever technology is appropriatein your project / company context !

Page 26: JavaFX Rest

Sébastien [email protected]://blogs.sun.com/sebsto http://www.twitter.com/sebsto

JavaFX Rich Internet Applicationswith RESTful web services,Jersey API and JSONYaJuG, July 6th