Spring Boot cheatsheet

Here is a Spring Boot cheatsheet:

Spring Boot Configuration


  • Cloud: https://start.spring.io/
  • CLI: https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/

Parent Pom (latest version):



plugins {     id 'org.springframework.boot' version '2.7.0' }

Basic Project creation with spring init:

spring init -dweb,jpa

Common starters:

  • web  Web applications using Spring MVC (Tomcat embedded)
  • test Spring test using JUnit, Hamcrest and Mockito
  • security Secured services with Spring Security
  • webflux WebFlux applications using Spring Framework’s Reactive Web
  • websocket WebSocket applications using Spring Framework’s WebSocket
  • data-jdbc Configured resources to use Spring Data JDBC
  • data-jpa Persist data in SQL stores with Java Persistence API
  • data-rest Web applications using Spring Data repositories over REST
  • actuator Production ready features using Spring’s Actuator (monitor and manage)

Spring Boot Coding

 Base class:

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

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {

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


RestController example (RequestMapping)

public class CustomerController {
  @RequestMapping("/") public List <Customer> findAll() {
    List < Customer > customerList = new ArrayList < Customer > ();
    customerList.add(new Customer(1, "frank"));
    customerList.add(new Customer(2, "john"));
    return customerList;

RestController (GetMapping & PostMapping)

public class CustomerController {
  CustomerRepository repository;
  public List<Customer> findAll() {
    return repository.getData();
  public Customer findOne(@PathVariable int id) {
    return repository.getData().get(id);
  @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<Customer> create(@RequestBody Customer customer) {
    return new ResponseEntity<>(customer, HttpStatus.CREATED);

Test Class example (RestAssured):

package com.example.testrest;

import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static io.restassured.RestAssured.get;
import static org.hamcrest.CoreMatchers.is;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplicationTests {
  public void testCustomerList() {
    get("http://localhost:8080/list").then().assertThat().statusCode(200).body("size()", is(2));
        .body("name", Matchers.equalTo("frank"));
        .body("name", Matchers.equalTo("john"));