Java JSON RESTful Web Service Example

In this example we shall learn implementing Restful Web Service in Java where the data interchange format shall be JSON.

1. Introduction to RESTful Web Services

RESTful Web Services follow REST architecture which stands for REpresentational State Transfer. RESTful web services are light weight and highly scalable is one of the most common way to create APIs on web.

In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs). REST uses various representations to represent a resource like text, JSON and XML.

2. HTTP Methods

In REST, resources are manipulated using a fixed set of four HTTP create, read, update, delete operations: PUT, GET, POST, and DELETE.

  1. GET – Retrieves current state of a resource.
  2. PUT – Creates a new resource.
  3. DELETE – Deletes a resource.
  4. POST – Updates an existing resource or creates a new resource if it doesn’t exist.

3. HTTP Methods

Following is the sample usage of HTTP methods with RESTful Web Service. We shall be implementing the GET HTTP method in our example where data interchange format shall be JSON.

S.No. HTTP Method URI Operation
1 GET /JavaCodeGeeks/AuthorService/authors Get list of all authors
2 GET /JavaCodeGeeks/AuthorService/authors/1 Get author with id 1
3 PUT /JavaCodeGeeks/AuthorService/authors/2 Insert author with id 2
4 POST /JavaCodeGeeks/AuthorService/authors/2 Update author with id 2
5 DELETE /JavaCodeGeeks/AuthorService/authors/1 Delete author with id 1

4. Requirements

For implementing this example, following is the requirement:

  1. Eclipse for Java EE.
  2. Apache Tomcat.
  3. Jersey Library can be downloaded from here.
  4. Google Chrome with any REST Client extension like Advanced REST client installed.
  5. Genson library for JSON conversion can be downloaded from here.

5. Project Setup

To start with the project setup, first create Dynamic Web Project in Eclipse.

Create Dynamic Web Project
Create Dynamic Web Project

Configure a web server like Apache Tomcat in your Eclipse environment. In my case, I had already configured the server, for which I also got an option against Target Runtime to choose while creating new Dynamic Web Project as shown above.

Now copy all libraries downloaded form Jersey‘s and Genson‘s website to WEB-INF/lib folder.

Copy Jersey and Genson Libraries
Copy Jersey and Genson Libraries

With all this, project setup is complete and now we shall learn implementing RESTful Web Services.

6. Implementing RESTful Web Service

First we shall configure Jersey to serve as servlet dispatcher for servlet requests. To do this we shall modify web.xml as follows.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	id="WebApp_ID" version="3.0">
			<param-value>com.javacodegeeks.examples.jersey, com.jersey.jaxb, com.fasterxml.jackson.jaxrs.json</param-value>

In web.xml, notice the configuration of ServletContainer and how packages folder has been configured that shall be searched for web service implementation.

Next we shall implement a POJO class whose object we shall return as a JSON via REST GET api.

package com.javacodegeeks.examples.jersey;

import javax.xml.bind.annotation.XmlRootElement;

public class Person {
	private int id;
	private String name;

	public int getId() {
		return id;

	public void setId(int id) { = id;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public Person(int id, String name) {
		super(); = id; = name;

	public Person() {

Now comes the last step in implementing web service.

package com.javacodegeeks.examples.jersey;

import java.util.ArrayList;
import java.util.List;


public class AuthorService {
	public List getTrackInJSON() {
		List listPerson = new ArrayList();
		Person p1 = new Person();
		Person p2 = new Person();
		return listPerson;

Notice the line of code @Produces(MediaType.APPLICATION_JSON) above. With this we are telling the web service that response shall be of type JSON.

Now that implementation of REST Web Service with GET HTTP Method is complete, we shall start the Tomcat server which we configured in Target Runtime above.

7. Testing RESTful Web Service

Now that implementation of web service is complete, we shall be testing the same now.

To test the web service open Google Chrome, open Developer Tools, and go to Network tab.

Chrome Developer Tools
Chrome Developer Tools

Now enter following URL in Address Bar and press Enter:


Notice that in Developer Tools Network tab, a new request will appear. Click on it and then click on Response tab within.

REST Response
REST Response

Here we can see a JSON response as returned from web service.

8. Download the Source Code

This was an example of Java JSON RESTful Web Service.

You can download the full source code of this example here: JSONRestWebServiceExample

Saurabh Arora

Saurabh graduated with an engineering degree in Information Technology from YMCA Institute of Engineering, India. He is SCJP, OCWCD certified and currently working as Technical Lead with one of the biggest service based firms and is involved in projects extensively using Java and JEE technologies. He has worked in E-Commerce, Banking and Telecom domain.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button