Track C · Tutorial 1

Spring Boot Fundamentals

Chapter 26 · Backend Specialization
~3 hours Intermediate Java · Spring MVC · JPA · DI

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.

Why this matters: Spring Boot is the standard for enterprise Java development. Mastering it opens doors to countless job opportunities and large‑scale projects.

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.
// Main application class @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

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

my-api/ ├── src/main/java/com/example/api/ │ ├── ApiApplication.java │ ├── controller/ │ ├── model/ │ └── repository/ ├── src/main/resources/ │ └── application.properties └── pom.xml (or build.gradle)

3. Spring MVC & REST Controllers

Spring MVC provides the web layer. Use @RestController for REST APIs.

// ProductController.java @RestController @RequestMapping("/api/products") public class ProductController { @GetMapping public List getAllProducts() { // return product list } @GetMapping("/{id}") public Product getProduct(@PathVariable Long id) { // return single product } @PostMapping public Product createProduct(@RequestBody Product product) { // create and return product } }

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.

// Product.java (Entity) @Entity public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Double price; // getters and setters } // ProductRepository.java @Repository public interface ProductRepository extends JpaRepository { // Custom queries List findByNameContaining(String name); }
// Using the repository in a service @Service public class ProductService { @Autowired private ProductRepository productRepository; public List findAll() { return productRepository.findAll(); } }

5. Dependency Injection

Spring's IoC container manages beans. Use @Autowired to inject dependencies.

// Constructor injection (recommended) @RestController public class ProductController { private final ProductService productService; public ProductController(ProductService productService) { this.productService = productService; } @GetMapping public List getAll() { return productService.findAll(); } }
Best practice: Prefer constructor injection over field injection for better testability and immutability.

Quiz

Question 1

Which annotation marks a class as a Spring Boot application entry point?

  • @SpringApplication
  • @SpringBootApplication
  • @EnableAutoConfiguration
  • @Application
Show answer
B. @SpringBootApplication.

Question 2

Which annotation is used to define a REST controller in Spring Boot?

  • @Controller
  • @RestController
  • @Service
  • @Component
Show answer
B. @RestController.

Question 3

Which interface does Spring Data JPA provide for basic CRUD operations?

  • CrudRepository
  • JpaRepository
  • Repository
  • All of the above
Show answer
D. All of the above (CrudRepository is the base, JpaRepository extends it).

Exercises

Exercise 1

Create a Category entity with id, name, and description. Write a CategoryRepository interface.

Sample answer
@Entity public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String description; // getters and setters } @Repository public interface CategoryRepository extends JpaRepository { }

Exercise 2

Add a PUT /api/products/{id} endpoint that updates an existing product.

Sample answer
@PutMapping("/{id}") public Product updateProduct(@PathVariable Long id, @RequestBody Product product) { return productService.update(id, product); }

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.