← Back to Home

Module 2: Spring Boot

Learning Objectives

Introduction to Spring Boot

Spring Boot is an extension of the Spring framework that simplifies the building and development of applications. It takes an opinionated approach to configuration, automatically configuring your application based on the dependencies you've added to your project. This allows you to get started with minimal setup and configuration.

Spring Boot makes it easier to create stand-alone, production-grade Spring based applications that you can "just run". It's designed to be fast, require minimal configuration, and provide a platform to create microservices and cloud-ready applications.

REST APIs and Spring

REST (Representational State Transfer) APIs provide a way to expose data and business logic safely through endpoints or routes. Spring Boot is an excellent framework for building REST APIs because it provides built-in support for HTTP methods, request routing, and response handling.

A REST API typically follows the CRUD (Create, Read, Update, Delete) operations through HTTP methods:

Key Topics

Spring Boot Basics

Learn the core concepts and components of Spring Boot.

  • Auto-configuration
  • Starters and dependencies
  • Spring Boot application structure

RESTful Web Services

Create and manage RESTful APIs with Spring Boot.

  • Creating controllers
  • Mapping HTTP requests
  • Request parameters and path variables

Dependency Injection

Understand Spring's IoC container and dependency injection.

  • Spring beans
  • Component scanning
  • Using annotations like @Autowired

Spring Boot Configuration

Learn how to configure and customize Spring Boot applications.

  • Property files
  • Profiles
  • Externalized configuration

Building REST Services with Spring Boot

Setting Up a Spring Boot Application

To create a Spring Boot application, you need to add the necessary dependencies to your build.gradle file:

implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

Then, create your main application class with the @SpringBootApplication annotation:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Creating a REST Controller

Controllers in Spring Boot handle HTTP requests. Here's an example of a simple REST controller:

@RestController
public class DogController {
    
    @Autowired
    private DogRepository dogRepos;

    @GetMapping(value = "/dogs/name/{findname}", produces = {"application/json"})
    public ResponseEntity<?> findADogs(@PathVariable String findname) {
        return new ResponseEntity<>(dogRepos.findByName(findname), HttpStatus.OK);
    }
}

Understanding Spring Boot Annotations

Different Types of Request Parameters

Spring Boot supports different ways to handle request parameters:

Path Variables

Used in URL paths like /dogs/name/dottie, where "dottie" is the path variable.

@GetMapping(value = "/dogs/name/{findname}")
public ResponseEntity<?> findADogs(@PathVariable String findname) {
    // Method implementation
}

Request Parameters

Added to the URL with a question mark like /dogs/name?name=dottie.

@GetMapping(value = "/dogs/name")
public ResponseEntity<?> findADogs(@RequestParam(name = "findname", required = false) String theName) {
    // Method implementation
}

Key Benefits of Spring Boot

Resources

Spring Boot Code-Along Starter

Starter code for the Spring Boot code-along exercise.

Spring Boot Code-Along Solution

Solution code for the Spring Boot code-along exercise.

Spring Boot Example Repository

Example Spring Boot application with dogs API.

Code-Alongs

Additional code-along exercises for this sprint.

Sprint Challenge

Access the sprint challenge for this unit.