Wednesday 17 September 2014

Creating Jersey RESTful Webservice Using Maven ,Eclipse and Apache Tomcat

This tutorial explains how to develop Jersey RESTful Webservice using Maven, Eclipse and Apache Tomcat.

Pre- Requisite:

Java:

Download and install the latest version of Java. Note the version used in this articale is Java 1.8.0_05.

Apache Tomcat:

Download and install the latest version of Apache Tomcat.  Note the version used in this article is 7.0.54.

Eclipse:

Download and Install the latest version of Eclipse Kepler Eclipse. Note the version used in this article is 4.3

Maven

Download and Install the latest version of Maven. Note the version used in this article is 3.2.1.

Setting up Eclipse:

Configure Maven in Eclipse:

EclipseAlready has build-in maven support but i have added the maven which i downloaded in Eclipse so that my maven from command-line and eclipse point to the same.

Go to Windows ---> Preference in Eclipse. Select Maven --> Installations from the popup.
Click Add and select the folder where Maven is Installed. 
(Below is how it looks after adding the external Maven. I have installed maven in C:\apache-maven-3.2.1 folder)

Configure Tomcat in Eclipse:

Go to Windows ---> Preference in Eclipse.Select Server -- > Runtime Environments.Click add and Select Apache --> Apache Tomcat v7.0 in popup.

Click Next and Select the Tomcat installation directory that you have installed. Click finish.


Creating the Simple Jersey RESTful Webservice Using Maven.

1.Open Command Prompt and Navigate to the Folder/WorkSpace where you want to create the project.

2.Execute the below Command to create the Jersey Project.


mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.12

3.After Downloading the required repository maven will ask for values for below properties

PropertyValue
groupId
com.ranvanshi.jersey
artifactId

FirstDemo
version
1.0
package
com.ranvanshi.jersey.first

4.After you Confirm the Jersey project is created.

Importing the Maven Project in Eclipse.

1. Open Eclipse if not already open.Select File --> Import. And select Maven -->Existing Maven Project


2. Click Next and Browser the Project that you have created and click Finsh.

3. There will be JSP Problem error in the project. To solve this error Right Click the project and Select Properties. Then Select Project Facets and select the Runtime Tab on Right Side. And Select the Tomcat Server That we have added earlier and Apply changes and click OK button.


Run the Project

Right Click the project and Select Run As --> Run on Server. Select the Server and click Finish.
Server will start and you application will be deployed.

Open any browser and go to the below link http://localhost:8080/FirstDemo/
You will See the index page for the application.
Click on the Jersey resource to access the Jesery REST service.

Conclusion:

We have seen how to create a Jersey RESTful webservice using Maven, Eclipse and Apache Tomcat. This tutorial doesn't explain about the details of the Jersey RESTful webservice instead explains about the how to set up the environment for developing and create a sample application.
 

No comments:

Post a Comment