Sunteți pe pagina 1din 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 Overview
JavaFX in a nutshell
REST based Web Service
JSON
Put it all Together

2009 CommunityOne Conference: North | no.sun.com/communityone 3


Agenda
Architecture Overview
JavaFX in a nutshell
REST based Web Service
JSON
Put it all Together

2009 CommunityOne Conference: North | no.sun.com/communityone 4


Architecture Overview
RIA Leveraging existing backend services

JavaFX REST
Client RIA Web Service
http / json jdbc
GlassFish v3

2009 CommunityOne Conference: North | no.sun.com/communityone 5


Agenda
Architecture Overview
JavaFX in a nutshell
REST based Web Service
JSON
Put it all Together

2009 CommunityOne Conference: North | no.sun.com/communityone 6


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

2009 CommunityOne Conference: North | no.sun.com/communityone 7


JavaFX Code Sample

Declarative, Object Oriented


Stage, Scene
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 Overview
JavaFX in a nutshell
REST based Web Services
JSON
Put it all Together

2009 CommunityOne Conference: North | no.sun.com/communityone 9


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

2009 CommunityOne Conference: North | no.sun.com/communityone 10


RESTful web services : a Java API

JSR 311, aka aka JAX-RS


Jersey is JAX-RS Reference Implementation
RESTful 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

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 Overview
JavaFX in a nutshell
REST based Web Services
JSON
Put it all Together

2009 CommunityOne Conference: North | no.sun.com/communityone 13


JSON, JavaScript Object Notation

A data format to exchange data structures


Language independent
Mainly 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"
]
} 2009 CommunityOne Conference: North | no.sun.com/communityone 14
JSON vs XML

JSON is more CPU, memory & network efficient*


• XML $ jar tfv classes.jar | grep xml | wc -l
3563
• JSON $ ls -al json/*.java | wc -l
7

XML has much more to offer in terms of semantics


XML Ubiquity

It all depends to your project size, team and …


the architect's decision

* 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 Overview
JavaFX in a nutshell
REST based Web Services
JSON
Put it all together

2009 CommunityOne Conference: North | no.sun.com/communityone 16


Putting it all Together
The Big Picture

REST
Web Service
http / json
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)


Return an array with 4 random integer
{
"Values" : [ 5,99,42,20 ]
}
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: [ ... ]
}
}
}
Reusing JavaFX 1.2 PieChart component
Alternative : Open Source PieChartFX + online
tutorial to build it from scratch
• 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


onInput 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


Notice special JavaFX constructs
• 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


JavaFX application polls web service every 5000ms

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
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 straightforward
Use whatever technology is appropriate
in your project / company context !

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
sebastien.stormacq@sun.com
http://blogs.sun.com/sebsto http://www.twitter.com/sebsto

S-ar putea să vă placă și