Java Reference
In-Depth Information
REST
The remainder of this chapter is about calling REST-based web services. Plenty of resources and documentation about
REST and REST-based web services can be found on the Internet. REST-based web services expose a number of URIs
that can be accessed using the HTTP protocol. Typically, different HTTP request methods (get, post, put, delete) are
used to indicate different operations on resources.
REST-based web services can be accessed using standard HTTP technologies, and the Java Platform comes with a
number of APIs (mainly in java.io and java.net ) that facilitate access to REST-based web services.
One of the major advantages of JavaFX being written on top of the Java Platform, Standard Edition 8, is the
ability to use all of these APIs in JavaFX applications. This is what we do in the first examples in this chapter. We show
how we can use Java APIs for consuming REST-based web services and how we can integrate the result in a JavaFX
application.
Next, we show how to leverage the JavaFX APIs to avoid common pitfalls (e.g., unresponsive applications, no
dynamic update, etc.). Finally, we give a brief overview of third-party libraries that make it easy for JavaFX developers
to access REST-based web services.
Setting Up the Application
First of all, we create the framework for our samples. We will use the APIs provided by Stack Exchange. The Stack
Exchange network is a cluster of forums, each in a specific domain, where questions and answers are combined
in such a way that the “best” answers from the most trusted users bubble to the top. Java developers are probably
familiar with StackOverflow, which was the first site in Stack Exchange and provides an incredible number of
questions and related answers for IT-related issues.
in the previous edition of this topic, Pro JavaFX 2, we used the twitter api in the examples for this chapter.
the twitter api is still available, and it is very good example of how to use rest services. however, all requests to the
twitter api now need to be authenticated. although this is fully understandable, it makes it a bit harder to get started
with a simple example. as you will see in this chapter, we start by making a simple HTTPConnection to a rest endpoint,
and we don't worry about authentication. the frameworks we introduce near the end of the chapter, are capable of using
Oauth-based authentication, and the connection to the twitter api is left as an exercise for the interested developer.
Note
The REST APIs provided by StackExchange are very well described at https://api.stackexchange.com . It is not
our goal to explore all the possibilities offered by StackExchange and the corresponding APIs, so the interested reader
is referred to the documentation available on the web site.
In the samples in this chapter, we want to visualize the author of a question, the title of the question, and the day
the question was asked.
Initially, we represent a question by a Java Object with getters and setters. This is shown in Listing 11-1.
Listing 11-1. Question Class
package projavafx;
public class Question {
private String owner;
private String question;
private long timestamp;
 
 
Search WWH ::




Custom Search