JavaFX Rest
description
Transcript of 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
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.
32009 CommunityOne Conference: North | no.sun.com/communityone
Agenda
Architecture OverviewJavaFX in a nutshellREST based Web ServiceJSONPut it all Together
42009 CommunityOne Conference: North | no.sun.com/communityone
Agenda
Architecture OverviewJavaFX in a nutshellREST based Web ServiceJSONPut it all Together
52009 CommunityOne Conference: North | no.sun.com/communityone
Architecture OverviewRIA Leveraging existing backend services
GlassFish v3
JavaFXClient RIA
RESTWeb Service
jdbchttp / json
62009 CommunityOne Conference: North | no.sun.com/communityone
Agenda
Architecture OverviewJavaFX in a nutshellREST based Web ServiceJSONPut it all Together
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
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" } }}
92009 CommunityOne Conference: North | no.sun.com/communityone
Agenda
Architecture OverviewJavaFX in a nutshellREST based Web ServicesJSONPut it all Together
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
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
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"; }}
132009 CommunityOne Conference: North | no.sun.com/communityone
Agenda
Architecture OverviewJavaFX in a nutshellREST based Web ServicesJSONPut it all Together
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" ] }
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
162009 CommunityOne Conference: North | no.sun.com/communityone
Agenda
Architecture OverviewJavaFX in a nutshellREST based Web ServicesJSONPut it all together
172009 CommunityOne Conference: North | no.sun.com/communityone
Putting it all TogetherThe Big Picture
GlassFish v3
RESTWeb Service
http / json
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 ]}
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; }}
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
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
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
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
242009 CommunityOne Conference: North | no.sun.com/communityone
DemoIt is not just slideware … it really works !
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 !
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