Spring Boot Fundamentals
Overview
Spring Boot is the leading Java framework for building production‑ready applications. This tutorial covers the essentials: setting up a project, building REST APIs with Spring MVC, using Spring Data JPA for database access, and understanding dependency injection.
1. Introduction to Spring Boot
Spring Boot is built on top of the Spring Framework. It provides:
- Auto‑configuration: Automatically configures beans based on dependencies.
- Starter dependencies: Pre‑configured dependency sets (e.g., `spring-boot-starter-web`).
- Embedded server: Tomcat, Jetty, or Undertow built‑in.
- Production‑ready features: Actuator, metrics, health checks.
2. Project Setup
Use Spring Initializr (start.spring.io) to generate a project.
- Project: Maven or Gradle
- Language: Java
- Spring Boot: 3.1.x (latest stable)
- Dependencies: Spring Web, Spring Data JPA, H2 Database (or PostgreSQL)
Project Structure
3. Spring MVC & REST Controllers
Spring MVC provides the web layer. Use @RestController for REST APIs.
Annotations:
@RestController, @RequestMapping,
@GetMapping, @PostMapping, @PathVariable,
@RequestBody.
4. Spring Data JPA
JPA (Java Persistence API) maps Java objects to database tables. Spring Data JPA provides a repository layer with CRUD methods.
5. Dependency Injection
Spring's IoC container manages beans. Use @Autowired to inject dependencies.
Quiz
Question 1
Which annotation marks a class as a Spring Boot application entry point?
- @SpringApplication
- @SpringBootApplication
- @EnableAutoConfiguration
- @Application
Show answer
Question 2
Which annotation is used to define a REST controller in Spring Boot?
- @Controller
- @RestController
- @Service
- @Component
Show answer
Question 3
Which interface does Spring Data JPA provide for basic CRUD operations?
- CrudRepository
- JpaRepository
- Repository
- All of the above
Show answer
Exercises
Exercise 1
Create a Category entity with id, name, and description. Write
a CategoryRepository interface.
Sample answer
Exercise 2
Add a PUT /api/products/{id} endpoint that updates an existing
product.
Sample answer
Homework
Homework 1
Build a complete CRUD API for an Order entity with fields: id,
productName, quantity, orderDate. Use Spring Data JPA and a service layer.
Sample outline
- Entity: Order (id, productName, quantity, orderDate)
- Repository: OrderRepository
- Service: OrderService with findAll, findById, save, update, delete
- Controller: OrderController with all CRUD endpoints
- Test: Use Postman or curl to verify endpoints
Mini‑Project
Bookstore API
Build a Bookstore API with Spring Boot:
- Book: id, title, author, isbn, price, stock
- CRUD endpoints (GET, POST, PUT, DELETE)
- Search endpoint: GET /api/books/search?author=...&title=...
- Use H2 database (in‑memory) for development
Sample implementation outline
- Model: Book entity with JPA annotations
- Repository: BookRepository extends JpaRepository
- Service: BookService with business logic (search, stock checks)
- Controller: BookController with all endpoints
- application.properties: spring.h2.console.enabled=true
Tutorial Summary
You learned the fundamentals of Spring Boot: project setup, building REST APIs with Spring MVC, data persistence with Spring Data JPA, and dependency injection. You built a solid foundation for enterprise Java development.
Key takeaway: Spring Boot's auto‑configuration and starter dependencies eliminate boilerplate, allowing you to focus on business logic. The DI container makes your code modular and testable.