728x90 AdSpace

Saturday, 3 August 2013

Hello World RESTful Web service

Create new Dynamic Web Project and  give project name as RESTful_HelloWorld

Copy all jar files into WEB-INF/lib folder.Select build path add jar in your build path.

Create Class

Create package called com.javatutorialscorner.rest.ws
Create following class inside package com.javatutorialscorner.rest.ws
RestHelloWorld .java
package com.javatutorialscorner.rest.ws;

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

public class RestHelloWorld {

public String sayHello(){
return "Welcome to Java Tutorials Corner";


This class called by using base url + path and get resource using HTTP GET method its is specified by annotation @GET and it produce the Sting output as  Plain Text output and @PATH define the class URI
now your project directory structure look like this 
project directory

Configure Jersey Servlet dispatcher

you need to register REST as servlet dispatcher in web.xml.Configure the following content in web.xml







This is servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer.The init param com.sun.jersey.config.property.packages is used to defines in which package jersey will look for the web service classes.This package point to your resource class package.URL pattern is part of your base URL.Now your web service is read to run.Your Web Service available at following url
http://your domain:port/Project Name/url pattern/path


open browser enter the mention url you can see the following output
We can able to call GET method alone via browsers other methods of http should be called via client application

Create Client

you can create and run your client anywhere as per your wish in this example I created client inside same package
package com.javatutorialscorner.rest.ws;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class HelloWorldClient {

public static void main(String[] args) {
String baseURI = "http://localhost:8080/RESTful_HelloWorld";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(baseURI);




If you run this client you will get the following out in console
Welcome to Java Tutorials Corner

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


  1. This comment has been removed by a blog administrator.


Item Reviewed: Hello World RESTful Web service Rating: 5 Reviewed By: Annamalai Thangaraj