This article is part 1 of a four-part series. It briefly introduces basic Representational State Transfer (REST) principles, and then quickly leaves theory in order to build a real-life sample application. We will use the new Java Platform, Enterprise Edition (Java EE) 6 standard to create a classical JavaServer Faces application, and we will study why this application does not follow the REST principles. Using NetBeans, we will then add a second, Jersey-based view on top of our application, which will run in parallel with the JavaServer Faces front end. We will then compare both approaches to determine their pros and cons.
In part 2 of this series, we will look at JQuery, a popular JavaScript framework. In parts 3 and 4, we will then plug our JQuery code on top of our REST-based application to create a modern, state-of-the-art, dynamic Website.
Note: This article was written and tested using NetBeans IDE 6.8.
REST is an architectural style defined by Roy T. Fielding in his doctoral dissertation, which has been widely used and discussed during the last decade.
It is beyond the scope of this article to describe REST in detail, but to summarize, the idea is that clients and servers communicate by sending and receiving representations of resources. REST is closely tied to HTTP. (Fielding was one of the main authors of the HTTP protocol and of the Apache Web server.) As an example, you could use the following:
This might look fairly low-level to a Java developer. In fact, most Java EE applications, especially JavaServer Faces applications, do not follow this architectural style, and that's why a large part of this article is dedicated to comparing JavaServer Faces and REST applications.
REST is just an architectural style, not a technology. That's why there is a Java specification (JSR 311) to describe how REST should be implemented in Java.
There have been several implementations of this standard. Jersey is its official reference implementation and the one that is most widely used in development and production. Jersey is also open source software, and it is backed by Oracle.
We are going to build the sample application step by step, but if you want to take a look at the final application, you can download its source code here.
Our sample application is an application to vote for articles. An article has an author, and there can be several votes per article. As such, an article has three entities (or resources, to use REST terms): Article, Author, and Vote.
Let's code this application in five minutes using NetBeans and JavaServer Faces.
We just generated create, read, update, and delete (CRUD) pages on top of three entities. Our application is fairly basic, but it's working correctly.
Let's start by creating two authors and then clicking the "list all authors" link. Here is the resulting page:

Note that the URL is http://localhost:8080/ArticleEvaluator/faces/jsf/author/Create.xhtml.
You can also see the same page if you click the "list all authors" link from the first page:

The problem is that the URL on this page is http://localhost:8080/ArticleEvaluator/faces/index.xhtml.
As you can see, while our application is perfectly correct from an end-user point of view, its URLs do not represent the entities that are being worked on. Indeed, for finding all authors in a REST way, we would have issued a GET request on the http://localhost:8080/ArticleEvaluator/resources/authors URL.
There's also a second problem with our application: It can work only with JavaServer Faces views. A true REST application can have several representations of the same resource, for example, in XML, HTML, PDF, JavaScript Object Notation (JSON), and so on.
Let's add a second, REST-based front end to our application. NetBeans can create all this automatically:
NetBeans generates two sets of classes:
The three resources Author, Article, and Vote are now available through a REST interface. If you look at the classes in the resources package, you can see how the URLs are constructed as well as the type of representations that are available for those resources: application/xml and application/json.
Because we already created two authors in the previous section, test the URL http://localhost:8080/ArticleEvaluator/resources/authors. A Web browser asks for an XML or HTML representation of a resource; that's why the application sends back the application/xml representation of the AuthorsResource:

As you can see from this XML code, it is possible to browse through the different AuthorResource instances by using the URLs that are provided. For example, using the http://localhost:8080/ArticleEvaluator/resources/authors/1/ URL, you can find the first author as well as the list of articles he has written.
Testing our application using only a Web browser is not enough, especially because we would like to use the JSON representation of our resources.
JSON is widely used in REST-based applications, because this format is more compact than XML and because it can easily be used with most common programming languages, including JavaScript. This is the format we will use in the next articles of this series, when we use our REST resources with JQuery.
We have two options for testing our application:
For a normal project, the first approach is recommended, but to get a better view of the internals of a REST application, we will use the second approach. We will use Apache Commons HttpClient, which is a widely used, open source, Java HTTP client from the Apache Software Foundation. To use this library, you must download it from http://hc.apache.org/httpclient-3.x/ and add it to your project's classpath.
In the Test Packages menu node in NetBeans, create a new package called fr.responcia.otn.articleevaluator.rest.resource, and create in this package a new class called ArticleTest. Here is the code for this class:
package fr.responcia.otn.articleevaluator.rest.resource;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test class for the Article REST front-end.
* @author Julien Dubois
*/
public class ArticleTest {
private static final String url = "http://localhost:8080/ArticleEvaluator/resources/";
@Test
public void createArticle() throws Exception {
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
PostMethod method = new PostMethod(url + "articles");
String xml = "<article><title>Jersey Article</title><text>text</text></article>";
RequestEntity requestEntity = new StringRequestEntity(xml, "application/xml", "UTF-8");
method.setRequestEntity(requestEntity);
client.executeMethod(method);
String responseBody = method.getResponseBodyAsString();
method.releaseConnection();
assertEquals("", responseBody);
}
@Test
public void testResourceAsXml() throws Exception {
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
HttpMethod method = new GetMethod(url + "articles");
client.executeMethod(method);
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
method.releaseConnection();
assertTrue(responseBody.contains("<title>Jersey Article</title>"));
}
@Test
public void testResourceAsJson() throws Exception {
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
HttpMethod method = new GetMethod(url + "articles");
method.addRequestHeader("Accept", "application/json");
client.executeMethod(method);
String responseBody = method.getResponseBodyAsString();
System.out.println(responseBody);
method.releaseConnection();
assertTrue(responseBody.contains("\"title\":\"Jersey Article\""));
}
}
This class has three methods:
Because the last two methods output their results in the console, you can see there are two very different representations of the article, one in XML and one in JSON. As previously stated, the JSON version is smaller (493 characters) than the XML one (587 characters).
We created a JavaServer Faces and a REST front end for our application. We have seen a number of common aspects:
Now let's see the differences between the two approaches:
We have seen that NetBeans helps us develop very quickly JavaServer Faces and REST-based applications. Both front ends can be used together. However, the JavaServer Faces application can be developed more quickly, especially if it is a business application used inside a company, while the REST front end works better for an open, scalable, publicly available Web application.
The completed sample application for this article can be downloaded here: sample-application.zip
In the next article of this series, we will look at JQuery, a widely popular JavaScript framework. In articles 3 and 4, we will then plug our JQuery code on top of our REST-based application in order to create a modern, state-of-the-art, dynamic Website.
