LAB 5557
Develop a JavaFX
Application, with RESTful
Web Service and JSON
David Delabassée
Sébastien Stormacq
Sun Microsystems, Northern Europe
Instructor-Led Hands-on Labs
> Instructors will provide background for exercises
> Exercises are self-paced
Hard-copy and online lab guides are available
Use suggested durations as a guide
> Raise your hand at any time for assistance
> To get the most out of the lab...
Read the lab guide, especially the background
Don't just copy and paste the solutions
JavaONE 2009 | sun.com/javaone 2
Housekeeping
> Before you leave, please fill out a survey and
hand it to someone
We really want to know what you think!
> Please log out of your machine when done
> Please look around to make sure you have all of
your belongings
> The hard copies of the lab guides are yours to
keep
JavaONE 2009 | sun.com/javaone 3
Architecture Overview
RIA Leveraging existing backend services
REST
Web Service
http / json
GlassFish v3
JavaONE 2009 | sun.com/javaone 4
JavaFX in a nutshell
> Scripting, declarative, object oriented 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
Samples
Adobe Illustrator & Photoshop plugins
> Built on top of Java™ platform
JavaONE 2009 | sun.com/javaone 5
REST Architecture
> 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
JavaONE 2009 | sun.com/javaone 6
RESTful web services
> REST 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
JavaONE 2009 | sun.com/javaone 7
RESTful Web Services
> JSR 311, aka aka JAX-RS
Will be part of upcoming Java EE 6 specification
> 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)
> Jersey is JAX-RS Reference Implementation
JavaONE 2009 | sun.com/javaone 8
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"
]
} JavaONE 2009 | sun.com/javaone 9
Exercises
> Exercise 1: Create a JavaFX Application
● Expected duration: 20 minutes
> Exercise 2: Create a RESTful web service
● Expected duration: 30 minutes
> Exercise 3: Asynchronous calls in JavaFX
● Expected duration: 30 minutes
> Exercise 4: Polling the web service
● Expected duration: 20 minutes
> Lab built on JavaFX 1.1
JavaONE 2009 | sun.com/javaone 10
Getting Started
> If you have not logged in, log in with
username: lab5557
password: hol009
> Online lab guide will open in a browser window
> All necessary software and lab files are already
installed on your lab machine
Start from exercise 1
JavaONE 2009 | sun.com/javaone 11
Exercise 1
> Create your first JavaFX application
Create the project
Build, Run and Test
> Add an existing PieChart component
Modify the application
Build Run and Test
> PieChartFX is open source
Online tutorial to build it from scratch
http://blogs.sun.com/sebsto
JavaONE 2009 | sun.com/javaone 12
Exercise 2
> Create a Web Application
Create, Build, Run and Test
> Add a RESTful web service
Will return text/plain data type
Build, Run and Test
> Add a method that will return JSON data
Will return application/json data type
Build, Run and Test
JavaONE 2009 | sun.com/javaone 13
Exercise 3
> Return to the JavaFX Application
> Add an asynchronous call to the RESTful web
service
> Use Java based JSON parser to parse the result
var request : RemoteTextDocument = RemoteTextDocument {
url: "http://host/MyWebService/resources/values";
}
var value: String = bind request.document on replace {
if (request.done) {
//do something with value
}
}
JavaONE 2009 | sun.com/javaone 14
Exercise 4
> Add polling capabilities to JavaFX
Reuse existing java.util.Timer class
Poll every 5000 ms
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);
JavaONE 2009 | sun.com/javaone 15
Congratulations!
> You should now have completed this lab
> If you would like more time to continue working,
please consider taking the lab exercises with you
Discs containing all of the labs offered this year are
available for you to take home
The lab guide will tell you where to get help with this
lab after JavaOne
> Thank you for attending this hands-on lab!
JavaONE 2009 | sun.com/javaone 16
David Delabassée
david.delabassee@sun.com
Sébastien Stormacq
sebastien.stormacq@sun.com
Sun Microsystems, Northen Europe
JavaONE 2009 | sun.com/javaone 17