How to Make an Application Using Spring Boot (Server-side)

Bennett Holiday
Analytics Vidhya
Published in
4 min readSep 29, 2021

Spring Boot is a Java-based framework that enables agile software development and accelerates business value delivery.

Introduction

Spring Boot is a convenient tool for rapidly developing any Spring application. It simplifies the process of developing stand-alone, production-grade Spring-based applications that “just run”. It has preset configurations that save you time and avoid maintenance of a large amount of boilerplate code.

In this tutorial, we develop a Hello World application and experiment with some microservices options.

Tools Preparation

Download and install the tools provided below according to your operating system.

  1. Java Development Toolkit
    Spring Boot requires Java JDK for the compilation and execution of Spring Boot code. For detailed instructions on how to download and install JDK, click here.
  2. Maven
    Maven is an automatic package management tool for Spring Boot. For detailed instructions on how to download and install Maven, click here.

Note: Do not forget to configure Java and Maven environment variables. For complete installation instructions and setting of environment variables, click here.

After installing Java and Maven successfully and correctly specifying path to the environment variables, run mvn -v on the Command Prompt/Terminal/Console and inspect the output.

Java and Maven successful installation on a Windows environment

Spring Boot code is composed of multiple packages that provide a wide range of functionalities, relieving developers of the load of web service implementation. As a result, developers may focus on the design and deployment mechanisms of their applications. As such, we’ll demonstrate how it works in this tutorial by running a small web application.

Task-I: Start and Terminate a Project

  • Go to Spring Initializr.
  • Generate a Maven Project with Spring Boot (2.5.5) and Java (JDK 17). Keep default values for Project Metadata as follows.
Spring initializr generate project
  • Unzip the file in any directory. In this tutorial, the directory name is demo.
  • Look at pom.xml for dependency management.
  • Open Command Prompt/Terminal/Console.
  • Navigate to the path you unzip the file and get into directory demo.
  • Run the application using the command mvn spring-boot:run.
Navigate to the unzipped file “demo” and run the application
  • Press CTRL + C to terminate the running application.
Terminate the application run

Task-II: Add Controller

The DemoApplication object in the program initiated the application’s execution, however it has not yet done any operations. To complete the process, we construct a Controller that will handle requests and a json file that will process the responses. Continue with the instructions as follows:

  • Add following dependencies into the pom.xml file:
Dependencies for pom.xml
  • Move Greeting.java, GreetingController.java, and MailUser.java files to demo/src/main/java/com/example/demo directory.
  • Move json file to demo/src/main/resources directory.
  • Navigate to the path of directory demo.
  • Run the application using the command mvn spring-boot:run.
  • Open your default browser and use the following web-links.
http://localhost:8080/get_mailshttp://localhost:8080/set_mail?mail=demo@demo.com
  • The localhost web-link get_mails will show the emails recorded in the database while set_mail will add a new email demo@demo.com in the database.
Get mails
Set mail
Search newly added mail
  • Press CTRL + C to terminate the application run.

Task-III: Change Configuration of Spring Boot

Spring Boot supports a variety of configuration options. Now, we will change the port configuration in such a way that ordinary users cannot access certain services.

  • Navigate to demo/src/main/resources directory.
  • Add the following code to application.properties.
server.port=8081management.port=8082
  • Run the application using the command mvn spring-boot:run.
  • Open your default browser and use the following web-link.
http://localhost:8081/greeting
Greeting message

Congratulations! You built a simple web application with Spring Boot and learnt how it may dramatically speed up your development pace. Additionally, you toyed with a few features to test their utility. This tutorial is a brief review of the capabilities of Spring Boot.

Conclusion

In this tutorial, we learnt how to initialize and configure a simple web application using Spring Boot. I hope you have experienced its flexibility on practice.

All the code files for Tasks I, II, and III are available at GitHub.

Thanks for reading! If you enjoyed this piece, a quick 👏 would mean the world to me. Stay in the loop by subscribing to my profile and don’t hesitate to comment or reach out. Happy reading!

--

--

Bennett Holiday
Analytics Vidhya

Research scientist, freelance writer, forever curious. Research is my playground, sharing insights is my passion.