Templates by BIGtheme NET

RESTFul Web Service explained in 5 minutes

In this Article, I will show How to create, build, deploy and test RestFul Web Service.

Tools and technologies Uses :

1) jersey-bom – 2.26-b01
2) jersey-container-servlet-core
3) eclipse version Luna 4.4.1.
4) Maven 4.0.0
5) apache-tomcat-8.0.22
6) JDK 1.6 or above

Steps to be followed :

1) Create Maven RESTFul Project in Eclipse.
2) Build the web project RESTFullWSQuickStart.
3) Deploy the project RESTFullWSQuickStart in to server (tomcat).
4) Demo

1) Create Maven RESTFul Project in Eclipse:

In eclipse, Go to File->New->Other and Select Maven->Maven Project.

The project creation wizard will open up. Select Jersey provided archetype ‘jersey-quickstart-webapp’
with groupId ‘org.glassfish.jersey.archetypes’.


Click on next button, In next window provide the groupId as “com.devjavasource.restfull”
and artifactId as “RESTFullWSQuickStart”.


And click on Finish, then maven project will be created.

In this application, we have total number of three resources pom.xml, MyResource.java and web.xml file.

This is the maven file,

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">





            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        <!-- uncomment this to get JSON support

Here MyResource is a POJO class, In order to make this as service.
We need to add three annotations @Path, @Get and @Produces.
1) @Path – Represents the web application path, In our case it is “myresource”.
The value of the annotation is the URI relative to the web application context, as deployed in the web server.
In our example MyResource.getIt() will act as a RESTFul service.
2) @Get – Web request type Get.
3) @Produces – Response content type.

package com.devjavasource.restfull.RESTFullWSQuickStart;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

 * Root resource (exposed at "myresource" path)
public class MyResource {

     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     * @return String that will be returned as a text/plain response.
    public String getIt() {        
    	return "Response is from - RESTFull Webserverces Quick Start...";


<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-name>Jersey Web Application</servlet-name>


    <h2>RESTfull Web Service Quick Start!!!!</h2>
    <p><a href="webapi/myresource">Click Me To get Response</a>
    <p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
    for more information on Jersey!

2) Build the web project RESTFullWSQuickStart:
This is same as how we are building maven project, right click on pom.xml file and click on “maven instal”.


Once the project is build Successfully, Then RESTFullWSQuickStart.war file will be created.


3) Deploy the project RESTFullWSQuickStart in to server (tomcat):
Add RESTFullWSQuickStart.war to the server and start the server.



4) Demo:

Go to browser and provide the URL “http://localhost:8080/RESTFullWSQuickStart/”


Click on the link “Click Me To get Response” to get the response from service “MyResource.java” class.


You can download complete project, Here


*** Venkat – Happy learning ****