RESTful Web Services - Environment Setup

RESTful Web Services - Environment Setup

This tutorial will show you how to prepare your development environment to work with the Jersey Framework to create RESTful web services. The Jersey framework implements the JAX-RS 2.0 API , which is a standard specification for building RESTful web services. This tutorial will also teach you how to set up the JDK, Tomcat and Eclipse on your machine before setting up the Jersey Framework.

Setting up the Java Development Kit (JDK)

You can download the latest SDK from Oracle's Java site - Java SE Downloads . You will find instructions for installing the JDK in the downloaded files. Follow the installation and setup instructions. Finally, set the PATH and JAVA_HOME environment variables to point to the directory that contains Java and Javac , usually java_install_dir/bin and java_install_dir respectively.

If you are on Windows and have installed the JDK in C:\jdk1.7.0_75 you will need to put the following line in your C:\autoexec.bat file.

set PATH = C:\jdk1.7.0_75\bin;%PATH%
set JAVA_HOME = C:\jdk1.7.0_75

Alternatively, on Windows NT/2000/XP, you can also right-click My Computer → select Properties → then Advanced → then Environment Variables. You will then update the PATH value and click OK.

On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.7.0_75 and you are using the C shell, you should put the following in your .cshrc file.

setenv PATH /usr/local/jdk1.7.0_75/bin:$PATH
setenv JAVA_HOME /usr/local/jdk1.7.0_75

Alternatively, if you are using an Integrated Development Environment (IDE) such as Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, compile and run a simple program to confirm that the IDE knows where you installed Java, otherwise case, perform the correct setting according to this document. from the IDE.

Setting up the Eclipse IDE

All examples in this tutorial were written using the Eclipse IDE. So, I would advise you to install the latest version of Eclipse on your computer.

To install the Eclipse IDE, download the latest Eclipse binaries from https://www.eclipse.org/downloads/ . After downloading the installation, extract the binary distribution to a convenient location. For example in C:\eclipse on Windows or /usr/local/eclipse on Linux/Unix and finally set PATH variable accordingly.

Eclipse can be launched by running the following commands on a Windows machine, or you can simply double-click on the eclipse.exe file.

%C:\eclipse\eclipse.exe

Eclipse can be started by running the following commands on a Unix (Solaris, Linux, etc.) machine:

$/usr/local/eclipse/eclipse 

After running successfully, if everything is ok, you should see the following output on your screen −

Eclipse Home Page

Installing the Jersey Framework Libraries

Now, if everything is in order, you can start setting up the Jersey framework. Below are a few simple steps to download and install the framework on your computer.

  • Choose whether you want to install Jersey on Windows or Unix and then proceed to the next step to download the .zip file for Windows and then the .tz file for Unix.

  • Download the latest Jersey platform binaries from the following link − https://jersey.java.net/download.html .

  • At the time of writing this guide, I have downloaded jaxrs-ri-2.17.zip to my windows machine and when you unzip the downloaded file it will give you a directory structure inside E:\jaxrs-ri-2.17\jaxrs-ri as shown in the following screenshot.

Choose whether you want to install Jersey on Windows or Unix and then proceed to the next step to download the .zip file for Windows and then the .tz file for Unix.

Download the latest Jersey platform binaries from the following link − https://jersey.java.net/download.html .

At the time of writing this guide, I have downloaded jaxrs-ri-2.17.zip to my windows machine and when you unzip the downloaded file it will give you a directory structure inside E:\jaxrs-ri-2.17\jaxrs-ri as shown in the following screenshot.

Jaxrs Directory

You will find all Jersey libraries in C:\jaxrs-ri-2.17\jaxrs-ri\lib and dependencies in C:\jaxrs-ri-2.17\jaxrs-ri\ext . Make sure you set the CLASSPATH variable in this directory correctly, otherwise you will run into a problem when running the application. If you are using Eclipse, you do not need to set the CLASSPATH because all settings will be done through Eclipse.

Configuring Apache Tomcat

You can download the latest version of Tomcat from https://tomcat.apache.org/ . After downloading the installation, extract the binary distribution to a convenient location. For example, in C:\apache-tomcat-7.0.59 on Windows or /usr/local/apache-tomcat-7.0.59 on Linux/Unix and set the CATALINA_HOME environment variable pointing to the installation locations.

Tomcat can be started by running the following commands on a Windows machine, or you can simply double-click the startup.bat file.

%CATALINA_HOME%\bin\startup.bat

or

C:\apache-tomcat-7.0.59\bin\startup.bat 

Tomcat can be started by running the following commands on a Unix (Solaris, Linux, etc.) machine:

$CATALINA_HOME/bin/startup.sh

or

/usr/local/apache-tomcat-7.0.59/bin/startup.sh

Upon successful startup, the default web applications included with Tomcat will be available at http://localhost:8080/ . If all is well, then the following output should be displayed −

cat

For more information about setting up and running Tomcat, see the documentation provided on this page. This information can also be found on the Tomcat website - https://tomcat.apache.org.

Tomcat can be stopped by running the following commands on a Windows machine −

%CATALINA_HOME%\bin\shutdown 

or

C:\apache-tomcat-7.0.59\bin\shutdown 

Tomcat can be stopped by running the following commands on a Unix machine (Solaris, Linux, etc.):

$CATALINA_HOME/bin/shutdown.sh 

or

/usr/local/apache-tomcat-7.0.59/bin/shutdown.sh

Once you're done with this last step, you're ready to move on to the first Jersey example you'll see in the next chapter.