Spring Boot Cheat Sheet
Annotations
@SpringBootApplication // main class
@RestController @RequestMapping("/api/x") // REST controller
@GetMapping("/{id}") @PostMapping @PutMapping @DeleteMapping
@PathVariable @RequestParam @RequestBody @Valid
@Service @Repository @Component @Configuration @Bean
@Transactional @Value("${key}")
JPA
@Entity @Table(name = "notes")
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false) @ManyToOne @OneToMany(mappedBy = "x")
interface Repo extends JpaRepository<Note, Long> {}
List<Note> findByTitleContainingIgnoreCase(String q);
repo.findAll(PageRequest.of(0, 20, Sort.by("id").descending()));
Errors and validation
@RestControllerAdvice @ExceptionHandler(NotFoundException.class)
@NotBlank @Size(min = 3) @Email @Min(1) @Positive
ResponseEntity.status(HttpStatus.CREATED).body(obj);
ResponseEntity.noContent().build();
Properties
# application.properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.jpa.hibernate.ddl-auto=update
spring.profiles.active=dev
management.endpoints.web.exposure.include=health,info
Build and run
./mvnw spring-boot:run
./mvnw test
./mvnw clean package
java -jar target/app.jar --spring.profiles.active=prod
Full Spring Boot documentation