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.

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.javabykiran.springrest</groupId>
<artifactId>JBKSpringRest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JBKSpringRest</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
	<artifactId>spring-data-rest-hal-browser</artifactId>
    </dependency>
    <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-devtools</artifactId>
	<scope>runtime</scope>
	<optional>true</optional>
    </dependency>
    <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
	<exclusions>
            <exclusion>
		<groupId>org.junit.vintage</groupId>
		<artifactId>junit-vintage-engine</artifactId>
            </exclusion>
	</exclusions>
    </dependency>
</dependencies>
<build>
    <plugins>
	<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
	</plugin>
    </plugins>
</build>
</project>

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.

JbkStudent.java

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.

JbkStudentController.java

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;

@RestController
public class JBKStudentController {
    @RequestMapping("/JBKStudentDetails")
    public List<JbkStudent> JbkStudentDetails() {
        ArrayList<JbkStudent> al = new ArrayList<JbkStudent>();
	JbkStudent ss = new JbkStudent();
	ss.setId(1);
	ss.setName("JavaByKiran");
	ss.setLoc("Pune");
	JbkStudent ss1 = new JbkStudent();
	ss1.setId(2);
	ss1.setName("JavaByKiran");
	ss1.setLoc("Nagpur");
        al.add(ss);
	al.add(ss1);
	return al;
    }
}

Spring Initializr is use to run our application.

JbkSpringRestApplication

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

@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