Invoke RESTful Web Services from JavaFX

avaFX, a new Sun Microsystems product family (including a scripting language, JavaFX Script), is optimized for implementing rich Internet applications for desktop and mobile devices. It comes bundled with the latest editions of the Java SE Runtime Environment (JRE) and Development Kit (JDK), and it provides its own API.

Although JavaFX Script uses syntax that somewhat resembles Java and JavaScript, it is important to understand how JavaFX differs from Java. One area in particular where this understanding would be helpful for Java developers who are just beginning to explore JavaFX is RESTful web services invocation. This 10-Minute Solution demonstrates how to invoke RESTful web services from JavaFX, and how to process the results.

As an example, it shows how to call a RESTful web service that provides information about airport departures and arrivals to a JavaFX front end. For the service URL, here is the abridged service data:

101DepartureUSFlairTYSATL


How do I call a RESTful web service from a JavaFX application, and how do I process a result?


Invoke a GETRESTful web service method and parse its results into a JavaFX object.

What You Need
JDK 6 Update 11
NetBeans 6.5 for JavaFX 1.0
For Eclipse users: JavaFX Plugin for Eclipse*
Developer References
Getting Started With JavaFX Technology
JavaFX 1.0 API | Overview | Java FX
* At this point, because of the newness of the product, NetBeans 6.5
provides the best support for JavaFX development.

Define a Container Data Structure

To begin, in a Flight.fxfile, define a JavaFX class that corresponds to the structure of the flight element:

public class Flight{ public var number: String; public var time: String; public var direction: String; public var carrier: String; public var destination: String; public var origin: String;} 

Accessing the Service

In a main JavaFX application (Main.fx) or in a location of your design, invoke a RESTful web service from a function loadFlightsSchedules using the JavaFX object HttpRequest (see Listing 1). All the pertinent properties and functions are declared within the declarative scope of HttpRequest.

Notice that the URL of the web service is specified in a location variable and the method GET (although JavaFX supports PUT and POST as well) is specified in the variable of the same name. You execute the call to the web service itself with request.enqueue();. If successful, the results are processed by the method onInput: function(input: java.io.InputStream).

Parsing the Results

In the function onInput: function(input: java.io.InputStream), Listing 1 handled the XML results from a web service by invoking the FlightsParser, which is a custom extension of the JavaFX object javafx.data.pull.PullParser. PullParsers supports direct query- and event-based XML parsing (similar to the SAX parser).

To load the elements from the parsed XML into a Flight object, you can use the (abbreviated) code in Listing 2.

You’re Done

This quick 10-Minute Solution has demonstrated how to use the GETmethod to invoke a RESTful web service and how to parse its results into a JavaFX object with minimal error handling.

For more details on web services processing and parsing of other data types, review the JavaFX objects javafx.io.http, javafx.data.pull, and javafx.data.xml.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a