Cyber Guard
Fight against cyber crime

Consume restful web service in spring boot application

RESTful web service producer and Consumer

Spring boot web service using Spring JPA
Spring boot web service producer and consumer

Producer of RESTful web service

  • Produces service as data in a structured format
  • The structured format can be XML, JSon , CSV, Excel or any
  • The producer provides an end point as Application Programming Interface (API)
  • The API must be available over internet protocol HTTP or HTTPS
  • The data available from RESTful web service is called as payload.

Refer to the following video to understand the process of producer Spring boot App.

Payload or output of the above video tutorial in Postman

Payload or output of Spring boot web service producer
Payload or output of Spring boot web service producer

How to Consume restful web service in spring boot application

1. Create a Spring boot Application as the consumer app. Use the following Links to know creation process a spring boot application in Eclipse or STS.

  • Creating Spring boot Application in Eclipse . Click here
  • Creating Spring boot Application in STS. Click here

2. Add Tomcat embed jasper and JSTL dependency in pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
<relativePath/> <!-- lookup parent from repository -->
<description>Demo project for Spring Boot</description>
<!-- -->
<!-- -->

3. Create a class to represent JSon name–value pairs.

package com.cyber.deb.boot.models;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
public class Patient {
int age;
String name;
public int getAge() {
return age;
public void setAge(int age) {
this.age = age;
public String getName() {
return name;
public void setName(String name) { = name;

4. Create a Controller to get data by using RestTemplate

package com.cyber.deb.boot.controllers;

import java.util.Arrays;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import com.cyber.deb.boot.models.Patient;

public class ConsumerController {
RestTemplate restTemplate=new RestTemplate();
public ModelAndView show()
      ModelAndView mv=new ModelAndView();
      ResponseEntity<Patient[]> responseEntity = restTemplate.getForEntity("http://localhost:8080/showPersons", Patient[].class);
      Patient[] responseBody = responseEntity.getBody()
      List<Patient> allPatient = Arrays.asList(responseBody);
      mv.addObject("myjson", allPatient);
      return mv;

5. Edit for changing default port and to specify jsp as the view


6. Add JSP file into WEB-INF/jsp folder in webapp.


<%@ taglib uri="" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Insert title here</title>
<div align="center">
<h1>WS Client</h1>
<table border="1">
<c:forEach items="${myjson}" var="mj">
 <td>${}</td><td> ${mj.age} </td>

Run the producer app in port 8080

Run the consumer app to view following response in web browser

Consume restful web service in spring boot application in web browser
Consume restful web service in spring boot application in web browser (Note it runs in port 8081)

Full Source code of producer App

Producer app zip download

Full Source code of Consumer App

Consumer app zip download


You may also like...

Leave a Reply

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