Spring Boot Cheatsheet

Setup

Use this Spring Boot reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Project Generation

Use this Spring Boot programming cheatsheet as a backend developer reference while you build Java REST APIs. For full-stack work, keep the React cheatsheet, SQL cheatsheet, Git cheatsheet, and Python cheatsheet nearby for UI, data, version control, and automation syntax.

Use Spring Initializr or the CLI to scaffold a project.

# Spring CLI (install via SDKMAN or brew)
spring init --dependencies=web,data-jpa,postgresql,validation,security \
  --build=maven --java-version=21 my-app

Initializr settings: - Project: Maven or Gradle (Kotlin DSL) - Language: Java 21+ (LTS) - Spring Boot: 3.x (latest stable) - Packaging: Jar (default); War only for servlet containers

Maven pom.xml Essentials

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.3.2</version>
</parent>

<dependencies>
  <!-- Web / REST -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <!-- JPA + Hibernate -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>

  <!-- Validation (Jakarta Bean Validation) -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
  </dependency>

  <!-- Security -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
  </dependency>

  <!-- PostgreSQL driver -->
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
  </dependency>

  <!-- Dev Tools (hot reload, disable caching in dev) -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
  </dependency>

  <!-- Tests -->
  <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>

Gradle (build.gradle.kts) Essentials

plugins {
    java
    id("org.springframework.boot") version "3.3.2"
    id("io.spring.dependency-management") version "1.1.5"
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-security")
    runtimeOnly("org.postgresql:postgresql")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

Main Application Class

package com.example.myapp;

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

@SpringBootApplication   // = @Configuration + @EnableAutoConfiguration + @ComponentScan
public class MyAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyAppApplication.class, args);
    }
}

@SpringBootApplication scans the package it lives in and all sub-packages. Put it at the root package.

Common Starters Reference

StarterBrings In
spring-boot-starter-webSpring MVC, Tomcat, Jackson
spring-boot-starter-webfluxProject Reactor, Netty (reactive)
spring-boot-starter-data-jpaHibernate, Spring Data
spring-boot-starter-data-mongodbMongoDB driver, Spring Data Mongo
spring-boot-starter-data-redisLettuce, Spring Data Redis
spring-boot-starter-securitySpring Security filter chain
spring-boot-starter-oauth2-resource-serverJWT / opaque-token validation
spring-boot-starter-validationHibernate Validator (Jakarta EE)
spring-boot-starter-actuatorHealth, metrics, info endpoints
spring-boot-starter-mailJavaMailSender
spring-boot-starter-cacheSpring Cache abstraction
spring-boot-starter-aopAspectJ / Spring AOP
spring-boot-starter-testJUnit 5, Mockito, AssertJ, MockMvc

Running the Application

# Maven
./mvnw spring-boot:run

# Gradle
./gradlew bootRun

# Fat JAR
./mvnw package
java -jar target/my-app-0.0.1-SNAPSHOT.jar

# Override properties at runtime
java -jar my-app.jar --server.port=9090 --spring.profiles.active=prod

Project Structure Convention

src/
  main/
    java/com/example/myapp/
      MyAppApplication.java     ← root (place @SpringBootApplication here)
      controller/               ← @RestController classes
      service/                  ← @Service classes
      repository/               ← @Repository / JPA interfaces
      model/                    ← @Entity + DTO classes
      config/                   ← @Configuration classes
      exception/                ← custom exceptions + @ControllerAdvice
    resources/
      application.properties    ← (or application.yml)
      application-dev.properties
      application-prod.properties
      static/                   ← served at "/"
      templates/                ← Thymeleaf / Mustache
  test/
    java/com/example/myapp/     ← mirrors main structure

Auto-configuration Gotchas

  • Auto-config fires only when a class/bean is not already defined — define your own bean to override.
  • Check what was configured: --debug flag prints a conditions report.
  • Exclude specific auto-config: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class).
  • spring-boot-devtools is not included in the fat JAR by default (optional=true).