devxlogo

Invoke RESTful Web Services from JavaFX

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist