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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as