Creating Our First Spring Boot Application: A Hands-On Guide

Bhawana Gaur
3 min readNov 30, 2023

--

Before we begin creating our first Spring Boot project, I would like to share that I’ve already published an article titled “Spring Boot: Simplifying Java Development”. This article covers the introduction of Spring Boot and its advantages. Therefore, I highly recommend reading it before we dive into the hands-on guide. Now let’s begin with our topic…

Introduction:

Welcome to our Spring Boot journey! In this article, we’ll dive into the practical aspects of building our very own Spring Boot application. We’ll cover setting up a simple project, understanding its structure, and running the application seamlessly.

Creating a Simple Spring Boot Project:

Let’s get started by creating a Spring Boot project using Spring Initializer. Follow these steps:

Step 1: Access Spring Initializer: Go to Spring Initializer.

Step 2: Project Configuration: Choose the project’s metadata like Group, Artifact, and dependencies (e.g., Web, JPA).

Step 3: Generate Project: Download the generated project ZIP file.

Alternatively, if you prefer using an IDE like IntelliJ IDEA or Eclipse:

Using IDE: Explore the IDE’s project creation wizard to set up a Spring Boot project, specifying metadata and dependencies.

Understanding the Project Structure:

Now that we have our project, let’s understand its structure:

└── src
└── main
├── java
│ └── com
│ └── example
│ └── myapplication
│ ├── MyApplication.java
│ ├── controllers
│ │ └── HomeController.java
│ ├── models
│ │ └── User.java
│ └── services
│ └── UserService.java
├── resources
│ ├── application.properties
│ └── static
│ └── css
│ └── styles.css
└── test
└── java
└── com
└── example
└── myapplication
├── controllers
│ └── HomeControllerTest.java
└── services
└── UserServiceTest.java

Java Source Code: Inside the src/main/java directory, we’ll find the main Java source code organized under packages.

Resources: The src/main/resources directory holds non-Java resources like properties files, static content, and templates.

Key Files: Highlight the MyApplication.java file annotated with @SpringBootApplication serving as the entry point and the build configuration file (pom.xml or build.gradle).

Controllers: Package for our application’s controller classes handling HTTP requests.

Models: Package for our application’s data models or entities.

Services: Package for service classes containing business logic.

Application.properties: Configuration file for Spring Boot application properties.

This structure demonstrates a typical arrangement for a Spring Boot project, with a clear separation of concerns through separate packages for controllers, services, models, and tests.

Running Our Spring Boot Application:

Using IDE: Launch the application directly from our IDE by following these simple steps:

  1. Locate the MyApplication.java file within your project.

2. Look for the main() method inside MyApplication.java.

3. Right-click on the main() method and select “Run” or “Debug” to start your application.

Using Command Line: If we prefer the command line, follow these steps:

  1. Open our terminal or command prompt.

2. Navigate to the root directory of our Spring Boot project.

3. For Maven, execute the command:

mvn spring-boot:run

4. For Gradle, run the command:

gradle bootRun

These methods will initiate the Spring Boot application. Once it’s up and running, we can access it through our web browser using the specified port (usually 8080 by default) at http://localhost:8080/.

Conclusion:

Congratulations on creating our first Spring Boot application! Today, we’ve successfully set up a basic project, delved into its structure, and witnessed the application running effortlessly. This marks an important step in our Spring Boot journey.

In the next post, we’ll explore the Basics of Spring Boot. So stay tuned and get ready to use the power of Spring Boot in our Java projects!

Happy coding!

--

--

Bhawana Gaur
Bhawana Gaur

No responses yet