Building Your First Java Spring Boot Application

Greetings, tech enthusiasts! Welcome to an in-depth tutorial blog designed to guide you through the process of building your first Java Spring Boot application. Whether you're a student embarking on a coding journey, a developer looking to expand skills, or simply curious about modern Java frameworks, this detailed walkthrough will help set up a testapp project using Spring Initializr.
Introduction: Understanding Spring Boot and Maven
Before diving into the hands-on setup, let’s establish a strong foundation by exploring the core technologies involved.
What is Spring Boot?
Definition: Spring Boot is an open-source framework extending the Spring ecosystem, aimed at simplifying the development of robust Java applications. It introduces auto-configuration, an embedded server (such as Apache Tomcat), and a rich set of tools to streamline the coding process.
Why Use It?:
Efficiency: Eliminates much of the boilerplate configuration, allowing focus on business logic rather than setup.
Rapid Development: Includes an embedded server, removing the need to deploy to an external container, which accelerates the development cycle.
Microservices Readiness: Perfect for building scalable, modular applications in a microservices architecture.
Community and Ecosystem: Benefits from extensive documentation, a large community, and a wide array of pre-built starters (e.g., for web, data, security).
Analogy: Imagine Spring Boot as a pre-prepared cake mix—add your unique ingredients (code) and bake it without configuring the oven manually. It’s a time-saver with all the essentials included!
What is Maven?
Definition: Maven is a powerful build automation tool for Java projects, utilizing a pom.xml file (Project Object Model) to manage dependencies, compile source code, run tests, and package the application.
Why Use It?:
Dependency Management: Automatically fetches libraries (e.g., Spring Boot starters) from repositories like Maven Central, ensuring version consistency.
Standardized Build Process: Provides a uniform build system across teams and environments, reducing setup discrepancies.
Integration: Seamlessly works with IDEs like IntelliJ IDEA and supports plugins for advanced tasks such as testing or deployment.
Analogy: Think of Maven as a detailed recipe book for a gourmet dish—it lists all ingredients (dependencies), specifies preparation steps, and ensures the kitchen (build environment) operates smoothly.
With these tools in mind, let’s proceed to create a Spring Boot project tailored for learning and experimentation.
Step 1: Setting Up Your Spring Boot Project
The creation of the testapp project begins with Spring Initializr, a web-based tool that generates a pre-configured project structure. Here’s a detailed breakdown of the setup process:
Accessing Spring Initializr
Navigate to https://start.spring.io, the official Spring Boot project generator.
Configure the following options:
Project: Select Maven as the build tool, known for its reliability and widespread use in Java development.
Language: Choose Java, the most established and widely adopted language for Spring applications.
Spring Boot Version: Opt for 3.5.3, the latest stable release as of June 2025, ensuring access to the newest features and security updates.
Java Version: Select 17, a Long-Term Support (LTS) version released in September 2021, offering modern features like sealed classes and enhanced performance.
Project Metadata:
Group: Enter com.testapp, representing a namespace or organizational identifier (often a reversed domain name).
Artifact: Set to testapp, defining the project’s unique name within the group.
Name: Use testapp for a human-readable project title.
Description: Add Test App to provide context about the project’s purpose.
Package Name: Automatically derives as com.testapp.testapp, serving as the base package for Java classes.
Packaging: Choose Jar for a standalone executable file, suitable for most applications (War is used for external server deployments).
Dependencies: Include the following to enhance functionality:
Spring Web: Enables RESTful web services with Spring MVC and an embedded Tomcat server.
MySQL Driver: Adds the MySQL JDBC driver for database connectivity.
Spring Boot DevTools: Provides live reload and automatic restarts for a smoother development experience.
Click the Generate button to download a ZIP file containing the project skeleton.
Importing into IntelliJ IDEA
Extract the downloaded ZIP file to a convenient location, such as the Downloads folder.
Open IntelliJ IDEA, select File > Open, and choose the pom.xml file within the unzipped folder.
Allow IntelliJ to import the project, download the specified dependencies, and set up the workspace. This process may take a few moments depending on internet speed.
Step 2: Exploring the Project Folder Structure in Detail
Once imported, the testapp project structure becomes visible in IntelliJ IDEA. Let’s take a thorough tour of each component to understand its role and purpose.
Root Directory: testapp
Location: Typically resides in ~/Downloads/testapp on a Unix-based system.
Purpose: Acts as the root directory, housing all project-related files and subdirectories.
Detailed Breakdown of Folders and Files
.mvn Folder
Contents: Includes wrapper, maven-wrapper.properties, mvnw, and mvnw.cmd.
Explanation: This folder contains the Maven Wrapper, a bundled version of Maven that ensures a consistent build environment. The maven-wrapper.properties file specifies the Maven version (e.g., 3.9.6), while mvnw (Unix) and mvnw.cmd (Windows) are executable scripts.
Usage: Execute ./mvnw spring-boot:run from the terminal to launch the application without requiring a globally installed Maven instance. This enhances portability across different machines.
src/main Folder
Purpose: Serves as the primary location for the application’s source code and resources.
Subfolders:
java/com.testapp.testapp
File:
TestAppApplication.java
Explanation: This file represents the application’s entry point. The ?
@SpringBootApplication
annotation combines@Configuration
,@EnableAutoConfiguration
, and@ComponentScan
, enabling auto-configuration, component scanning, and Spring context initialization.Code:java
package com.testapp.testapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TestAppApplication { public static void main(String[] args) { SpringApplication.run(TestAppApplication.class, args); } }
Usage: Running this class boots the Spring application, making it the starting point for execution.
resources
Subfolders/Files: Contains static, templates, and application.properties.
static: A directory for static assets such as CSS, JavaScript, and image files, which are served directly by the embedded server.
- Example Usage: Place a style.css file here to style web pages.
templates: Designed for dynamic HTML templates, supporting engines like Thymeleaf or Freemarker for server-side rendering.
- Example Usage: Add an index.html file for a dynamic welcome page.
application.properties: The configuration file for customizing application behavior.
Content Example:properties
server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=root spring.datasource.password=password
Usage: Modify this file to set the server port, database connections, or other properties. It starts empty or with defaults, offering flexibility for customization.
src/test Folder
Purpose: Houses test code to validate the application’s functionality.
Subfolder: java/com.testapp.testapp
File:
TestAppApplicationTests.java
Explanation: A JUnit 5 test class annotated with
@SpringBootTest
to verify the Spring application context loads successfully.Code: java
package com.testapp.testapp; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class TestAppApplicationTests { @Test void contextLoads() { } }
Usage: Execute this test to ensure the application context initializes without errors, serving as a basic health check.
Files: .gitattributes, .gitignore
Explanation: These are Git configuration files. .gitignore specifies files and directories (e.g., target/, IDE settings) to exclude from version control, while .gitattributes defines attributes for Git operations like line endings.
Usage: Maintains a clean repository by ignoring unnecessary files.
pom.xml
Explanation: The Maven Project Object Model file defines the project’s configuration, including dependencies, build plugins, and properties.
Content Example:xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" ...> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.5.3</version> </parent> <groupId>com.testapp</groupId> <artifactId>testapp</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Usage: Add or modify dependencies (e.g., Spring Data JPA, Lombok) to extend functionality. The spring-boot-maven-plugin enables executable JAR creation.
Step 3: Running and Testing the Application
Now, let’s bring the testapp to life with a detailed execution process.
Running the Application
In IntelliJ IDEA: Right-click TestAppApplication.java in the project explorer, then select “Run” or “Debug” from the context menu.
In Terminal: Navigate to the testapp directory using a command line interface and execute ./mvnw spring-boot:run (Unix) or mvnw spring-boot:run (Windows).
Result: The application launches, and the embedded Tomcat server starts listening on http://localhost:8080 by default. Console output confirms the startup process, including any configured port changes.
Adding a Simple Controller
Create a new package named controller under com.testapp.testapp.
Add a file named HelloController.java with the following content: java
package com.testapp.testapp.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Readers! Welcome to Spring Boot!"; } }
Explanation: The @RestController annotation marks this class as a controller, while @GetMapping("/hello") maps the /hello endpoint to the sayHello method, returning a simple string response.
Testing: Rerun the application and open a web browser or use a tool like curl (e.g., curl http://localhost:8080/hello) to verify the message appears.
Testing the Setup
Run TestAppApplicationTests.java by right-clicking the file in IntelliJ and selecting “Run” or using ./mvnw test in the terminal.
Purpose: The contextLoads test ensures the Spring application context initializes correctly, serving as a foundational check.
Next Steps: Expand test coverage by adding unit tests for controllers or services as the application grows.
Step 4: Expanding and Customizing the Project
To take the testapp further, consider these enhancements:
Customizing application.properties: Modify the file to adjust the server port (e.g., server.port=8081) or configure a MySQL database with detailed connection properties:
properties
spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=root spring.datasource.password=password spring.jpa.hibernate.ddl-auto=update
Adding Features: Integrate Spring Data JPA for database operations by adding the dependency to pom.xml:
xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
Then create entity classes and repositories.
Deployment: Package the application into an executable JAR using ./mvnw clean package. The resulting file (e.g., target/testapp-0.0.1-SNAPSHOT.jar) can be run with java -jar target/testapp-0.0.1-SNAPSHOT.jar, allowing deployment to a server or local testing.
Wrap-Up
There it is, readers!, a detailed Spring Boot testapp has been crafted using Maven, Java 17, and essential dependencies. The folder structure provides a clear organization of code, tests, and configurations, while Maven ensures a robust build process. Experiment with additional controllers, database integration, or security features, and share questions or experiences in the comments—feedback fuels this blog! Stay tuned for more in-depth tech explorations!
Follow for the latest coding insights and tutorials!
Subscribe to my newsletter
Read articles from Atul directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Atul
Atul
AI ML | Data Science | react | Java | SQL JavaScript 🔁 python