DBA > Articles

Introduction to Jersey—a Standard, Open Source REST Implementation

By: Julien Dubois
To read more DBA articles, visit http://dba.fyicenter.com/article/


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 using NetBeans IDE 6.8, and it is recommended that you use the same version to complete the sample application.

REST and Jersey

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:

* An HTTP GET request to receive a resource
* An HTTP POST request to create a new resource

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. Building the Sample Application Quickly with NetBeans and JavaServer Faces

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.

1. Launch NetBeans and create a new project.
2. Choose a Java Web > Web Application project type, and name this project ArticleEvaluator.
3. Keep the default settings: The server is Glassfish v3 Domain and the Java EE version is Java EE 6 Web.
4. In the Frameworks tab, choose only JavaServer Faces, and keep using the default library that comes with the server.
Your project should now be created.
5. Right-click your project and select New > Entity class.
This first entity is called Article and is in the package fr.responcia.otn.articleevaluator. You should see a warning indicating that you do not have a configured persistence unit yet.

6. Select Create Persistence Unit, keep the default options (especially the Use Java Transaction APIs option), and select a data source.

This data source is linked to a database,which could be the Java DB instance provided by Glassfish or an external database, such as Oracle or MySQL.

7. Now select Insert code > Add property, and add two properties, text and title, which are both of type String.
8. Add another entity called Author, which has three properties of type String: firstName, lastName, and url.
9. In the Article entity, add a property author of type Author.
10. Using the light bulb, create a bidirectional ManyToOne relationship between the Article and the Author entities. (An Article has one author, and an Author can write many Articles.)
The light bulb generates an articles list in the Author entity, but not in its accessors.
11. Open the Author entity, select Insert code > Getter and Setter, and generate the getter and setter for the articles list.
12. Create the last entity, Vote, which has a property stars of type int (that's the number of stars an article receives in one vote), a property ip of type String (that's the IP address of the user who votes, but it could instead be the user's login name), and a property article of type Article (that's another bidirectional ManyToOne relationship, because an Article can have many votes, whereas a Vote can be cast on only one Article).
13. As before, generate the getter and setter for the votes list that was generated in the Article entity.
14. In the Web Pages menu node, create a new folder called jsf by right-clicking the Web pages folder and selecting New > Other > Folder.
15. Right-click your project and select New > JSF Pages from Entity Classes.
16. On the second screen of the wizard, choose the jsf folder as the JSF pages folder.

NetBeans generates all the necessary files and configuration, and your application should be up and running.

A Closer Look at the Sample Application

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:

Full article...

Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/