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>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-parent</artifactId>       <version>2.1.8.RELEASE</version>       <relativePath/>   </parent>  <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-web</artifactId> </dependency>  <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-test</artifactId>       <scope>test</scope> </dependency> 


plugins {     id 'org.springframework.boot' version '2.1.8.RELEASE' } 

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:

// Enable component-scanning and auto-configuration with @SpringBootApplication Annotation // It combines @Configuration + @ComponentScan + @EnableAutoConfiguration @SpringBootApplication public class DemoApplication {   public static void main(String[] args) {     SpringApplication.run(DemoApplication.class, args);   } } 

RestController example

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; } }

Controller with Repository example

@Controller @RequestMapping("/") public class UserController {    private UserRepository userRepository;    @Autowired   public UserController(UserRepository userRepository) {     this.userRepository = userRepository;   } } 

Test Class example:

@RunWith(SpringJUnit4ClassRunner.class) // Load context via Spring Boot @SpringApplicationConfiguration(classes = ReadinglistApplication.class) @WebAppConfiguration public class ReadinglistApplicationTests {   // Test that the context successfully loads (the method can be empty -> the test will fail if the context cannot be loaded)   @Test   public void contextLoads() {   } }