How to Make an Application Using Spring Boot (Server-side)
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.
- Java Development Toolkit
Spring Boot requiresJava JDK
for the compilation and execution of Spring Boot code. For detailed instructions on how to download and install JDK, click here. - 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.
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.
- 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
.
- Press
CTRL + C
to terminate the running application.
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:
- Move
Greeting.java
,GreetingController.java
, andMailUser.java
files todemo/src/main/java/com/example/demo
directory. - Move
json
file todemo/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 whileset_mail
will add a new emaildemo@demo.com
in the database.
- 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
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!