Welcome to Day 2 of my Spring Boot learning journey! π
After understanding the fundamentals of Spring Boot on Day 1, today I moved from theory to practice by setting up my complete development environment and building my very first Spring Boot application.
π οΈ Development Environment
To begin my Spring Boot development, I set up the following tools:
- Eclipse IDE β For writing and managing Java code.
- MySQL β To store application data.
- Spring Boot β The backend framework.
- Maven β For dependency management and project building.
With everything installed and configured, I was finally ready to write some code.
π¦ Creating My First Entity: Product
The first class I created was a simple Product class.
It contains three fields:
idnameprice
Along with that, I manually wrote:
- Constructors
- Getters
- Setters
-
toString()method
At first glance, the code looks quite lengthy because Java requires a lot of boilerplate code for a simple class.
π‘ Note: In upcoming days, I'll learn Lombok, which automatically generates constructors, getters, setters, and other methods, making the code much cleaner and easier to maintain.
ποΈ Understanding Spring Boot Project Structure
To follow Spring Boot's layered architecture, I created three important layers:
π Controller Layer
The Controller acts as the entry point for client requests.
I created two REST endpoints:
GET /get-all
- Retrieves all products.
- Returns a list of products wrapped inside a
ResponseEntity. - Sends an HTTP 200 OK status.
POST /save-prod
- Accepts a Product object using
@RequestBody. - Saves the product.
- Returns the saved product with HTTP 201 CREATED.
Using ResponseEntity allows us to send both the response data and the appropriate HTTP status code.
π Repository Layer
Next, I created a ProductRepository.
Instead of writing SQL queries manually, I simply extended:
JpaRepository<Product, Integer>
This provides several built-in database operations like:
- save()
- findAll()
- findById()
- delete()
- update()
without writing any implementation code.
That's one of the biggest advantages of Spring Data JPA.
π Service Layer
The Service layer contains the application's business logic.
Here, I created methods to:
- Fetch all products.
- Save a new product.
The service communicates with the repository, while the controller communicates with the service.
This layered architecture keeps the code organized and easier to maintain.
π Request Flow
The complete flow of my application looks like this:
Client Request
β¬οΈ
Controller
β¬οΈ
Service
β¬οΈ
Repository
β¬οΈ
MySQL Database
β¬οΈ
Response Back to Client
This separation of responsibilities is one of the core principles of Spring Boot development.
π Concepts I Learned Today
β Setting up Eclipse for Spring Boot development
β Configuring MySQL
β Creating a Spring Boot project
β Understanding layered architecture
β Creating an Entity class
β Building a Repository using JPA
β Writing a Service layer
β
Creating REST APIs using @GetMapping and @PostMapping
β
Using @RequestBody
β
Returning responses with ResponseEntity
π My Takeaway
Today was my first real step into backend development with Spring Boot.
Although the project is simple, it helped me understand how different layers of a Spring Boot application communicate with each other. I also realized why Spring Boot is so popularβit lets developers focus on application logic instead of writing repetitive code.
There's still a lot to learn, but building something that actually runs and responds to API requests feels incredibly rewarding.
Tomorrow I'll continue exploring more Spring Boot concepts and improve this project step by step.
Thank you for following my journey! π
#SpringBootJourney Day 2
Learning never stops.
One concept.
One project.
One day at a time. π
Top comments (0)