Using JavaFX to Call Remote (Web) Services (Accessing Web Services) Part 6

DataFX

The DataFX library is described at http://javafxdata.org and it provides an end-to-end toolkit for retrieving, parsing, massaging, populating, viewing, and rendering data. These data might be obtained using REST-based web services, but can also be obtained from a local filesystem, a database, or a number of other datasources.

DataFX consists of two integrated parts:

Cell Factories, providing a number of useful CellFactories and hence reducing the boilerplate code that is often required in projects

• DataSources, providing a level of abstraction about the origin of the data, both regarding the physical location (file, network resources) and the format (JSON, XML, JDBC, etc.)

In the next example, we integrate DataFX with our Twitter example. Once again, the only change is in the getObservableList method, but for clarity, we show the whole main class in Listing 9-20.

Listing 9-20. Obtaining Tweets Using DataFX

Obtaining Tweets Using DataFX

 


 

 

Obtaining Tweets Using DataFX

 

The relevant part, the implementation of the getObservableList method, is very simple. We first construct a NetworkSource that points to the required REST endpoint. Next, we create an ObjectDataSource and use the builder pattern to do the following:

1. Assign the NetworkSource as the source of the data.

2. Indicate that we are interested in items that are defined as "item" elements.

3. Convert these items into instances of the Tweet class.

By calling the ObjectDataSource.retrieve method, an asynchronous Service is started. Instead of waiting for the result, we can immediately return the result object to our visual controls. The DataFX framework will update the result object while it reads and parses incoming data. For large chunks of data, this is very useful, because this approach allows developers to already render parts of the data while other parts are still coming in, or are still being processed.

Apart from the ObjectDataSource, DataFX has a number of other DataSources, including an XMLDataSource. Developers familiar with the Java XML APIs can use this DataSource and use xpath expressions in order to define what columns in a TableColumn should render what parts of the incoming XML.

Jersey-Client

Jersey is the reference implementation of JAX-RS. Although Jersey itself has no direct link to JavaFX, we mention it here for two reasons:

• Jersey-client provides some useful tools for calling REST-based web services.

• JAX-RS 2.0 will contain a client specification.

The JAX-RS 2.0 specification work is done in JSR 339 and the final release is expected in the second quarter of 2012. Jersey will provide the reference implementation for this JSR. At this moment, Jersey already provides the Reference Implementation for JAX-RS 1.x, which mainly defines the Server components of a REST implementation, but the client components will only be standardized in JAX-RS 2.0. Jersey uses the same principles we have shown in the examples in this topic (support for both JSON and XML, unmarshalling data to Objects, etc.).

Once the JAX-RS 2.0 specification is approved and the Jersey-Client reference implementation is available, it will provide useful tools for reducing the amount of boilerplate code. At this moment, one of the flavors of DataFX already uses Jersey-Client, and it is expected that more JavaFX Frameworks will leverage the standard Java 2 REST retrieving and parsing APIs that are defined in JAX-RS 2.0.

In the next example, we modify the TwitterRetrievalService of Listing 9-15 to use the Jersey-Client API. This is shown in Listing 9-21.

Listing 9-21. Using Jersey-Client for Retrieving Tweets

Using Jersey-Client for Retrieving Tweets

 

 

 

 

Using Jersey-Client for Retrieving Tweets

On this WebResource, we can call the get(Class clazz) method, and supply a class parameter. The result of the REST call will then be parsed into an instance of the supplied class, which is also what we did using JAXB in our example in Listing 9-15.

tmp4756_thumb[2][2][2][2]

The response now contains a list of Tweets, and we can use the exact same code as in Listing 9-15 to render the tweets.

Summary

In this topic, we explained briefly two options for integrating JavaFX applications and enterprise applications. We demonstrated a number of techniques for retrieving data available via web services, and also showed how to render the data in typical JavaFX controls as ListView and TableView.

We used a number of third-party tools that facilitate the process of retrieving, parsing, and rendering data. We demonstrated some JavaFX-specific issues related to remote web services (i.e., updating the user interface should happen on the JavaFX application thread).

It is important to realize that the decoupling between JavaFX client applications and web services allows for a large degree of freedom. There are different tools and techniques for dealing with web services, and developers are encouraged to use their favorite tools in their JavaFX application.

Next post:

Previous post: