Course Content
Spring Boot Backend
Spring Boot Backend
Project structure
Now we move on to the most exciting part, where we will run our first Spring Boot application using a practical example and break down the project structure.
We will be using IntelliJ IDEA, but make sure to use the Ultimate version to work with Spring Boot.
This version is paid, but IntelliJ IDEA offers a free one-month trial, allowing you to use the program without any issues!
If you'd like to use Spring Boot with the Community edition, here’s a link to an article explaining how to do it. Well, let's get started!
Project Creation
First, create a new project, and in the left panel, you will see Spring Boot — you need to select that.
Next, you should configure the project settings the same way I do. You can use any Java version, it's not crucial. Make sure to select Maven as the build type!
After clicking the Next
button, you will see a window showing the dependencies that will be included in our application.
You need to select Web
-> Spring Web
.
We need the Spring Web
dependency to develop web applications that handle HTTP requests, RESTful services( we'll cover that later ), and much more. It provides the foundational support for building web applications within the Spring
ecosystem, including features for request mapping, exception handling.
Click the Create
button to set up your project! Let's quickly go over the project structure.
Project Structure
At first, you'll have a structure like this, and here you'll find the pom.xml
file, which represents Maven. Let's take a look at it:
You might notice a lot of code that may seem unclear, but for now, the block we are interested in is inside the <dependencies>
</dependencies>
tags.
Here, we can add the dependencies we selected when creating the project (like Spring Web), this dependency is also referred to as a starter.
For example, the spring-boot-starter-web
starter includes all the necessary dependencies for building web applications, such as Spring MVC
and Tomcat
(a server on which our application runs), allowing developers to get started quickly.
After that, update the configuration by clicking this button. This will pull all the dependencies into the project.
There's also the src
directory, which, when expanded, reveals two more directories called main
and test
. The test
directory is used for writing tests, but we will cover that in other sections.
When we expand the main
directory, we see two more directories: java
and resources
.
The resources
directory is used to store all the resources for our website. HTML
files are kept in the templates
directory, while everything else (like CSS
, JS
files, and images) is stored in the static
folder. Additionally, there's the application.properties
file, where we specify extra project settings.
This file specifies the port on which the server will run, but you can change it, and the server will start on a different port. This file will be essential when we connect to a database, so we will study it in detail a bit later.
Finally, in the java
directory, you'll find the path to our main class.
Where does the Application Itself Launch?
In the screenshot, you can clearly see where the main class FirstSpringBootAppApplication
for running the application is located. Within the directory where the main class is found, you can write your own classes and run your applications.
Now, let's run our first application and check if everything works as expected.
If you have started the server and you see logs like these in the console, then congratulations, everything is working!
What If the Application Won’t Start?
When starting a Spring Boot application, you might encounter an error if the port is already in use. This happens when another process is using the port you've specified, preventing your application from binding to it. To fix this, simply change the port in the application.properties
file, for example, to 8081
.
You may also face issues with incorrect dependencies. If you forgot to include the necessary dependencies or specified incorrect versions in your pom.xml
, the application won’t be able to find the required classes. Make sure all dependencies are correctly defined.
Additionally, unavailable dependencies can hinder the loading of libraries due to issues with your internet connection or repository accessibility. Check your connection and ensure the repositories are reachable.
Finally, errors in your code, such as typos or incorrect annotations, can also cause startup failures. Check the logs for error messages and fix any issues you find.
Summary
In a Spring Boot project, the structure includes the main application class and configuration files, which are located in src/main/resources
. The focus is on simplifying configuration and enabling rapid deployment of the application.
Thanks for your feedback!