Spring 4 + REST Web Service + JSON Example with Tomcat

To perform this Program we must have knowledge of Spring Boot and REST API.

Refer below links to learn about Spring Boot and Spring Boot REST API.

  • In this chapter, we will learn how to use Spring 4 with REST Web Service to get JSON response.

  • A Spring 4 Web Service class is annotated with @RestController that replaces the use of @Controller and @ResponseBody.

  • To map the REST Web Service URL, use the annotation @RequestMapping.

  • Web service method argument contains @RequestParam that has the attribute defaultValue that will assign a default value for request parameter whose value is not available in request.

  • To setup the environment, we can use Spring Boot for fast startup. WebApplicationInitializer is being used to replace web.xml settings.

  • Spring configuration will be initialized by WebApplicationInitializer while on server startup.

Below softwares are needed to develop our Spring 4 + REST Web Service + JSON Example.

  1. Java 8

  2. Tomcat 7

  3. Eclipse

  4. Maven

In the following image we can see how the classes has been configured in eclipse for our spring 4 demo.

Structure of classes Spring Project in Eclipse

Spring Boot is the combination of Jar and Tomcat server. Spring Boot fetches all Jars required to run Spring 4 application. For developers, Spring Boot makes it easy to start development in easy and fast way. Spring Boot can be used with Gradle 1.11 or Maven 3.0.

In our demo project we have used maven. In our demo, we have a separate tomcat in which we deploy our WAR file externally. For JSON, Jackson API will be used. For Jackson dependency jackson-databind is used. Find the maven dependency.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    <relativePath /> <!-- lookup parent from repository -->
<description>Demo project for Spring Boot</description>



We have created a sample component for the example. This has a method that will return an object Person which will be changed in JSON format in response. Find the Person class which will be retuned as JSON response.


package com.javabykiran.springrest.JBKSpringRest;

public class JbkStudent {
    int id;
    String name;
    String loc;

    public int getId() {
	return id;

    public void setId(int id) {
	this.id = id;

    public String getName() {
	return name;

    public void setName(String name) {
	this.name = name;

    public String getLoc() {
	return loc;

    public void setLoc(String loc) {
	this.loc = loc;

Finally we implement our web service class which will be exposed for REST service. The class will be annotated with @RestController. Spring 4 has introduced @RestController annotation to replace @Controller and @ResponseBody. The annotation @RequestMapping can be used at class level as well as method level for REST Web Service URL mapping.


package com.javabykiran.springrest.JBKSpringRest;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class JBKStudentController {
    public List<JbkStudent> JbkStudentDetails() {
        ArrayList<JbkStudent> al = new ArrayList<JbkStudent>();
	JbkStudent ss = new JbkStudent();
	JbkStudent ss1 = new JbkStudent();
	return al;

Spring Initializr is use to run our application.


package com.javabykiran.springrest.JBKSpringRest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class JbkSpringRestApplication {
    public static void main(String[] args) {
	SpringApplication.run(JbkSpringRestApplication.class, args);
  • Run this application to see the output.

  • Open the browser and writer URL.

  • URL:- http://localhost:8080/JBKStudentDetails

  • Output:

  • Output of Spring JSON Project in localhost