Build RESTful Web service JASON data provider

Build RESTful Web service JASON data provider

This demo application will show you how to Build RESTful Web service JASON data provider Using JAX-RS and Jersey and test it with java client class.

Note: If you are not familiar with RESTful web service and need depth introduction on it before developing application please read this documentation: RESTful web service introduction

In this example, the method helloWorld() is annotated with @GET and the @Produces(“application/json”) annotation. This method will process JASON requests and produce content.

Below technology are needed to build application:

  • Eclipse Kepler
  • Java 1.6
  • Tomcat 7.0
  • JAX-RS 1.1
  • Jersey 1.8
  • Maven 3.0


  • Create maven project name: RESTfulXMLAndJavaClient (If you are not sure how to create maven project in eclipse please follow this tutorial: Create Maven project eclipse)
  • Configure Tomcat server in eclipse (If you are not sure please follow this tutorial: Configure Tomcat server in eclipse)
  • Now create package name: com.javahonk inside src/main/java folder
  • Create class name and copy paste below code:

package com.javahonk;


public class HelloWorld {

	//HTML Request
	public String helloWorld() {
		return "<html> " + "<title>" + "Hello World!" + "</title>"+ "<body><h1>" + "Hello World!" + "</body></h1>" + "</html> ";

	// JASON request
	public String sayJASONHello() {
		return "<?xml version=\"1.0\"?>"
				+ "<hello> Hello World from RESTful. It is APPLICATION_JSON request."
				+ "</hello>";

  • Now create java client class to test RESTful web service. Create class name inside package test and copy paste below code:

package test;



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 RESTfulTestClient {

	public static void main(String[] args) {
	    ClientConfig config = new DefaultClientConfig();
	    Client client = Client.create(config);
	    WebResource service = client.resource(getBaseURI());




	  private static URI getBaseURI() {
	    return UriBuilder.fromUri("http://localhost:8080/RestFulService/").build();

  • Now copy paste below code in your web.xml file:
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

  <display-name>First RESTful Hello World</display-name>





  • Copy below maven dependency jar into your pom.xml file:
<project xmlns="" xmlns:xsi=""
  <name>RestFulService Maven Webapp</name>
  • After all steps your project should look as below:

Build RESTful Web service JASON data provider

  • Now refresh and clean your project.
  • Deploy application on tomcat: Right click project –> Run As –> Run on Server
  • Now let’s test this with our java client RESTfulTestClient class. Run java test class: Right click java class –>Run As –> Java application. You will see below on your console. Great our test got successful.

Build RESTful Web service JASON data provider

download2 Download project: RESTfulXMLAndJavaClient

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *