Covid-19 Data is available from Word Health Organisation through different web serices. There are different aggregators application exist to filter the data of WHO and supply the data as RESTful web Service.We will use one of the aggregator for creating RESTful Spring boot Web application.
Watch following video to know creating of simple way of Consuming RESTful web service in Spring boot Web App
Requirement to Consume Covid-19 RESTful web service in Spring boot Web App
1. Create a Spring Boot starter web Application as the consumer.
- Creating Spring boot Application in Eclipse . Click here
- Creating Spring boot Application in STS. Click here
2. Edit pom.xml to add dependencies for Tomcat embed jasper and JSTL 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.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.consumer</groupId> <artifactId>Consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>Consumer</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </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> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.Change application.properties for adding jsp path
spring.mvc.view.prefix:/WEB-INF/jsp/ spring.mvc.view.suffix:.jsp
4. Identify public RESTful web service end point and create POJO classes.
whodata.java
package com.rest.consumer.models; import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties public class whodata { List<DateWise> cases_time_series; List<StateWise> statewise; public List<DateWise> getCases_time_series() { return cases_time_series; } public void setCases_time_series(List<DateWise> cases_time_series) { this.cases_time_series = cases_time_series; } public List<StateWise> getStatewise() { return statewise; } public void setStatewise(List<StateWise> statewise) { this.statewise = statewise; } }
DateWise.java
package com.rest.consumer.models; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties public class DateWise { String date; String totalconfirmed; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getTotalconfirmed() { return totalconfirmed; } public void setTotalconfirmed(String totalconfirmed) { this.totalconfirmed = totalconfirmed; } }
StateWise.java
package com.rest.consumer.models; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties public class StateWise { String lastupdatedtime; String state; String confirmed; String active; String deaths; public String getLastupdatedtime() { return lastupdatedtime; } public void setLastupdatedtime(String lastupdatedtime) { this.lastupdatedtime = lastupdatedtime; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getConfirmed() { return confirmed; } public void setConfirmed(String confirmed) { this.confirmed = confirmed; } public String getActive() { return active; } public void setActive(String active) { this.active = active; } public String getDeaths() { return deaths; } public void setDeaths(String deaths) { this.deaths = deaths; } }
5.Create a Controller to get JSon data from public RESTful end point by using RestTemplate
ConsumerController.java
package com.rest.consumer.controller; 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.rest.consumer.models.DateWise; import com.rest.consumer.models.StateWise; import com.rest.consumer.models.whodata; @Controller public class ConsumerController { RestTemplate restTemplate=new RestTemplate(); @RequestMapping("/indiadata") public ModelAndView data() { ModelAndView mv=new ModelAndView(); mv.setViewName("indiadata"); ResponseEntity<whodata> responseEntity = restTemplate.getForEntity("https://api.covid19india.org/data.json", whodata.class); whodata responseBody = responseEntity.getBody(); List<DateWise> allPatient = responseBody.getCases_time_series(); mv.addObject("myjson", allPatient); return mv; } @RequestMapping("/statedata") public ModelAndView stateData() { ModelAndView mv=new ModelAndView(); mv.setViewName("statedata"); ResponseEntity<whodata> responseEntity = restTemplate.getForEntity("https://api.covid19india.org/data.json", whodata.class); whodata responseBody = responseEntity.getBody(); List<StateWise> allStates = responseBody.getStatewise(); mv.addObject("myjson", allStates); return mv; } }
6.Create JSP files to represent data using JSTL tags. Use JQuery For pagination view of data
index.jsp (Landing page of the spring boot web app)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Covid19 Data Consumer</title> </head> <body> <h1 align="center"><a href="./indiadata">Date Wise Covid-19 Data in India</a></h1> <h1 align="center"><a href="./statedata">State Wise Covid-19 Data India</a></h1> </body> </html>
indiadata.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"> </head> <body> <div align="center"><h3>Date Wise Live Covid-19 Case in India</h3></div> <table id="example"> <thead> <tr><th>Date</th><th>Cases</th></tr> </thead> <tbody> <c:forEach items="${myjson}" var="mj"> <tr> <td>${mj.date}</td><td> ${mj.totalconfirmed} </td> </tr> </c:forEach> </tbody> </table> <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script> <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script> <script> $(function(){ $("#example").dataTable(); }) </script> </body> </html>
statedata.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"> </head> <body> <div align="center"><h3>State Wise Live Covid 19 Case in India</h3></div> <table id="example"> <thead> <tr><th>Last Updated</th><th>State</th><th>Confirmed</th><th>Active</th><th>Death</th></tr> </thead> <tbody> <c:forEach items="${myjson}" var="mj"> <tr> <td>${mj.lastupdatedtime}</td><td> ${mj.state} </td> <td>${mj.confirmed}</td><td> ${mj.active} </td> <td>${mj.deaths}</td> </tr> </c:forEach> </tbody> </table> <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script> <script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script> <script> $(function(){ $("#example").dataTable(); }) </script> </body> </html>
Project Explorer view of STS/Eclipse

Run the Project to see how to Consume Covid-19 RESTful web service in Spring boot Web App
Using localhost:8080 in web browser, output will appear from index.jsp


