× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Working with RESTful Web Services in Java

In this tutorial, we shall learn about RESTful web services and how they can be implemented in Java. We shall take a look on the background of RESTful web services and then move on to an example to demonstrate how it works.

APIs form an integral part of web-based applications these days. These are a set of protocols, tools and routines that are used for the building of software applications. They determine how an application should interact and are largely meant to be used for graphical user interfaces. RESTful web services, in this regard, are based on REST architecture and are largely meant for the creation of APIs.

What is REST?

REST, which expands to representational state transfer, is known to be an architectural style and is largely used for web development. The main aim of applications developed via REST is to offer fast performance and at the same possess the ability to quickly scale upwards. Usually, this is a style that largely suits the World Wide Web. So, REST is an architectural style that is used for software development on the World Wide Web.

REST mainly revolves around resource. In this aspect, every component is said to be a resource and can be accessed using a common interface that uses HTTP standard methods. Each resource is addressed by the use of URIs or global IDs. Text, JSON or even XML is used for the representation of resources.

Architectural properties of REST

The REST architectural style has the following properties –

  • Performance – Component interactions are a dominant factor when it comes to the performance and network efficiency.
  • Scalability is another property of REST. It supports a large number of components and interactions.
  • A Uniform Interface that is simple.
  • Components can be modified to suits changing needs.
  • Portable nature of the components.
  • Reliability and the resistance to failures.
  • Visibility in communication.

Advantages of REST

REST offers three major advantages. These are –

  • It is scalable.
  • It is light weight.
  • It can be maintained easily.


Before we delve into the examples of RESTful web service implementation, let us take a look into what one needs to have an idea about.

  • Java
  • HTML
  • CSS
  • AJAX
  • Text editors

What are RESTful web services?

A group of open protocols that are meant for the exchange of data between applications and systems is said to be a web service. Similarly, web services that are based on the REST architecture are known as RESTful web services.

Now, we shall move on to the example and see how one can work with RESTful web services and Java. We shall make use of the Jersey Framework to create these services. Jersey Framework is known to implement JAX-RS 2.0. This API is the standard specification for the creation of RESTful web services. We shall setup the JDK, Apache Tomcat, Eclipse and then the Jersey framework.

Setting up Java Development Kit

One can always bank on the official Oracle website to get the latest version of Java SDK. After installation of Java, an important step is to define the PATH. If you are using an IDE, then you must compile and run a simple program that lets you know that the IDE knows where Java is installed.

Setting up Eclipse IDE

Eclipse is one of the most used IDEs by coders for the development of applications. One can download Eclipse from the link http://www.eclipse.org/downloads/. The next step is to install the binaries and then set the CLASSPATH.

Setup Tomcat Server

The first step is to download the current Tomcat version from http://tomcat.apache.org/. After downloading, install the files in a location. On starting Tomcat, you may visit http://localhost:8080/, in order to check if it is set up fine.

Setup Jersey Framework

The final step is to have Jersey Framework libraries in your machine. Download the requisite files from the link https://jersey.java.net/download.html. Unpack the zipped files in your computer and then set the PATH.

After the completion of these steps, you should be ready to work with RESTful web services in Java.

We shall write a simple application using Jersey Framework that shall reveal a web service method to display a list of users.

Creation of Java Project

You are required to create a Dynamic Web Project using the Eclipse IDE. Select File, followed by New and then Project. Then select Dynamic Web Project. Give a name to your project, say UserManage, and click on Finish.

Adding libraries that are required

The second set up is to add the Jersey Framework and its libraries and dependencies that are needed in the project. Then you will need to copy the jar files of the download jersey zip folder in the directory WEB-INF/lib of the project, them being,

  • \jaxrs-ri-2.17\jaxrs-ri\api
  • \jaxrs-ri-2.17\jaxrs-ri\ext
  • \jaxrs-ri-2.17\jaxrs-ri\lib

Then you need to click UserManage, i.e. name of your project, and then follow up on the Build Path, Configure Build Path in the context menu. This will display the Java Build Path window.

Now, you can use the Add JARs option that’s available under the Libraries tab for the addition of JARs in the directory WEB-INF/lib.

Creation of Source Files

Now, we shall proceed with the creation of the source files for the project. However, before that we do need to create a package. We shall call it sample here. This can be done by right clicking on src in the package explorer and then choose New, followed by Package.

We shall now create Userserv.java, Usr.java and Userd.java under our package sample.

The User class is largely meant for the defining the various inputs from the user that we shall be using in our application. It defines input variables like that of id, name, and prof.

Listing 1: Sample class for Usr.java

package sample;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class Usr implements Serializable {
   private static final long serialVersionUID = 1L;
   private int id;
   private String name;
   private String prof;
   public Usr(){}   
   // Here we are initializing the Usr class. This is the constructor part of the class.
   public Usr(int id, String name, String prof){
      this.id = id;
      this.name = name;
      this.prof = prof;
   // Following are all getter and setter methods for populating and fetching the data of a Usr class.
   public int getid() {
      return id;
   public void setid(int id) {
      this.id = id;
   public String getname() {
      return name;
   public void setname(String name) {
      this.name = name;
   public String getprof() {
      return profession;
   public void setprofession(String prof) {
      this.prof = prof;

Next, we enter the details of the users, which will further make up the user list. We also perform a number of File Input/Output operations in this portion of the code.

Listing 2: Sample class Userd.java

package sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
public class Userd {
   public List<User> getAllUsers(){
      List<User> userList = null;
     //  Here, we are creating a file and populating Usr class. 
     try {
         File file = new File("Users.dat");
         if (!file.exists()) {
            Usr user = new Usr(145, "Rajesh", "Engineer");
            userList = new ArrayList<User>();
            FileInputStream fin = new FileInputStream(file);
            ObjectInputStream fout = new ObjectInputStream(fin);
            userList = (List<User>) fout.readObject();
      } catch (IOException ex) {
      } catch (ClassNotFoundException ex) {
      return userList;
  // In this method we are writing the data is a file named Users.dat  
 private void saveUserList(List<User> userList){
      try {
         File file = new File("Users.dat");
         FileOutputStream fost;
         fost = new FileOutputStream(file);
         ObjectOutputStream obos = new ObjectOutputStream(fost);
      } catch (FileNotFoundException ex) {
      } catch (IOException ex) {

This final class is used to obtain the list of users.

Listing 3: Sample Userserv.java

package sample;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
public class Userserv {
   Userd userdao = new Userd();
   public List<User> getUsers(){
      return userdao.getAllUsers();

There are two very important points to keep in mind with regards to the main program, which is Userserv.java.

  • It is necessary to specify a path for the web service by making use of the annotation @PATH to Userserv.
  • The second one is to specify the path for a certain web service method making use of the @Path annotation to the method of Userserv.

Creating the Web.xml configuration file

The next step is the creation of a web configuration file, an XML file that is meant for specifying the Jersey Framework servlet for our code.

Following is the web deployment descriptor for defining web properties. Here we are mentioning deployment related parameters.

Listing 4: Sample Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   id="WebApp_ID" version="3.0">
   <display-name>User Manage</display-name>
      <servlet-name> RESTful Application using Jersey </servlet-name>
   <servlet-name> RESTful Application using Jersey </servlet-name>

Deployment of the program

Once you have finished with the creation of the source and web configuration files, you are ready for the compilation and running of the project. In Eclipse, this can be done by exporting the application as a war file and then its deployment in Tomcat.

Creating a war file can be done by choosing File, export, Web and then War File. Then select the project name, UserManage, and the destination folder. Now, the deployment of the same in Tomcat can be done by placing the UserManage.war in the Tomcat Installation Directory. Then you can start Tomcat.

Running the code

This is the final step of the process where we check how the code runs. Here, we are making use of a Chrome extension called Postman for testing our web services.

Make a request to UserManage, in order to get a list of all the users. Place http://localhost:8080/UserManage/rest/Userserv/users with the GET request in Postman. You will get your results and that completes the project using RESTful web services application. Following will be the output.

Figure 1: Output of the web service call


The above designed tutorial takes on the background of REST and web services pertaining to it. We have learnt about the various features, aspects and advantages offered by RESTful web services. This was followed by learning how to setup the requisite software on which the project is based. Finally, we moved on to the coding aspect of the project and then ran it to see if it ran properly.

RESTful web services are the way for the creation of web applications and services and also used mainly in APIs. This tutorial will likely help you get started with RESTful web services in an easy manner.

Hope this example will help you build a RESTful web services for different applications.

Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login