Building a Coronavirus Case Tracer Application with Spring Boot and JAVA!

Anushka Shukla
Javarevisited
Published in
4 min readJul 16, 2021

In this blog, I’m going to explain how you can create your own coronavirus tracking app with Spring Boot and JAVA which reports the total number of cases around the globe. The cases are reported for each state in the country and then it also specifies the changes in the number of cases reported from the previous day.

This is how the application looks like.

If you get stuck don’t get worried you can see the source code available on my GitHub repository.

So let’s jump in to see what you need to know before building this application:

  1. You must be familiar with the language - JAVA.
  2. A little to no experience in Spring Boot will do.
  3. As this is going to be a web app, so you should know HTML/CSS.

Now that you’ve already taken a deep breath that you know the above-listed stuff already, let me tell you what you need to set up the environment to build this project.

1. JAVA 1.8+

2. Maven 3.2+

3. An IDE, you can choose IntelliJ IDEA or Eclipse.

Set Up a New Project

  1. Go to https://start.spring.io/ to generate a Maven with JAR packaging with the following dependencies:

For the rest of the other options, we can go with the default. Click on “Generate”. You can refer to the image below for your reference.

2. Extract the downloaded zip file and open it in your IDE. Choose the maven when you import it because it is a maven project.

3. Open the pom.xml file and make sure you’re using the correct JAVA version and make sure you have chosen the correct compiler for the same.

So now you’re done setting up the environment to build the project.

Data Source

We are building a web application so we need a data source or you can call it a data set. So we will use the repository which has the data provided by JHU CSSE. Whenever the Spring application loads a GET request is made to the mentioned repository’s csv file and your application will fetch all the data and parse it in a format that we create for our web page.

Let’s start building the app

So we have a main class and what we want to do is when this application runs it fetches the data.

package com.company.coronavirustracker;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication@EnableSchedulingpublic class CoronavirusTrackerApplication { public static void main(String[] args) { SpringApplication.run(CoronavirusTrackerApplication.class, args);
}
}

1. We’ll create a new JAVA class called services.CoronaVirusDataService. What we do here is create a method called fetchVirusData() which will make an HTTP request to the data URL and get a response as a string. This method will have a “@PostConstruct” annotation which means you can execute the method which makes the HTTP request once we are done creating the instance of the class “CoronaVirusDataService”. After this, you can run the application and check if you are getting the data printed in the console. It will be in a comma-separated text format.

Now we have to parse this csv data. Go to https://commons.apache.org/proper/commons-csv/ you will find a dependency as below and copy-paste it in your pom.xml file.

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>

And you can parse it by Header Auto Detection because in our data we have the first row that has headers like state, country, etc. We want to run this on a daily basis so what we do is add “@Scheduled” annotation to schedule the run of the method fetchVirusData() by cron expression and specify the string specifying how often we need to run it(min, hr, day, month).

2. Create a new class called models.LocationStats here we create the access modifiers and create getters and setters for the same. And now we can create a list of arrays in CoronaVirusDataService of LocationStats and create the instance forLocationStats and populate this list every time we get the request.

3. And now we want this data to be rendered in a nicely formatted way. So we’ll create a controller for this purpose, create a new JAVA class called controllers.HomeController and give it a “@Controller” annotation. Then create a method called home() which returns a template for the web page where the data will be rendered and it will be mapped to an HTML file.

4. Now create an HTML file in a template folder already existing. You can look to Thymeleaf’s documentation which can provide you the required code which you can simply copy-paste and edit according to how you want to present your web page. You can use Bootstrap which is a CSS framework to make it look nice and presentable without putting much of your head. And you can also add headings, colors etc. according to you.

5. Run the application and open http://localhost:8080/ in your browser.

And there you go, you have successfully completed your web application.

Yay!

If you liked it please do give it a clap. Do let me your feedback and suggestions so that I can improve by adding more to this blog.

Connect with me on LinkedIn, Twitter, or GitHub.

Thank you!

--

--