Skillversitas

Spring Boot Cheatsheet

Main Annotations

Annotation Description
@SpringBootApplication Entry point. Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan
@RestController Controller that returns data (not a view)
@Controller Conventional controller (returns a view/template)
@Service Mark a class as a service layer
@Repository Mark a class as a data access layer
@Component Mark a class as a Spring-managed bean
@Configuration Mark a class as configuration
@Bean Register a method's return value as a bean
@Autowired Inject dependencies automatically
@Qualifier Pick a specific bean when multiple beans exist

Web Annotations

Annotation Description
@RequestMapping Map an HTTP request to a method
@GetMapping Handle a GET request
@PostMapping Handle a POST request
@PutMapping Handle a PUT request
@DeleteMapping Handle a DELETE request
@PathVariable Get a value from the URL path
@RequestParam Get a query parameter
@RequestBody Bind the request body to an object
@Valid Validate input with Bean Validation

JPA Annotations

Annotation Description
@Entity Mark a class as a database table
@Table Set the table name
@Id Mark the primary key
@GeneratedValue Auto-increment ID
@Column Column configuration
@ManyToOne Many-to-one relationship
@OneToMany One-to-many relationship
@ManyToMany Many-to-many relationship
@JoinColumn Set the foreign key column

Testing Annotations

Annotation Description
@SpringBootTest Load the full Spring context
@WebMvcTest Test the controller only (slice test)
@DataJpaTest Test the repository with an embedded database
@MockBean Create a mock bean in the Spring context
@Autowired Inject a bean for testing
@BeforeEach Setup before each test
@Test Mark a method as a test
@ActiveProfiles Activate a profile for the test

Security Annotations

Annotation Description
@EnableWebSecurity Enable Spring Security
@EnableMethodSecurity Enable method-level security
@PreAuthorize Check role/permission before executing the method
@Secured Check roles with an annotation

Configuration Annotations

Annotation Description
@ConfigurationProperties Bind external configuration to an object
@Value Get a single property value
@Profile Activate a bean for a specific profile
@ConditionalOnProperty Bean only if the property matches

HTTP Methods

Method Description Idempotent
GET Fetch data Yes
POST Create new data No
PUT Update data (full replace) Yes
PATCH Update data (partial) No
DELETE Delete data Yes

HTTP Status Codes

Code Description
200 OK Success
201 Created Resource created successfully
204 No Content Success, without a response body
400 Bad Request Invalid request
401 Unauthorized Not logged in
403 Forbidden No access
404 Not Found Resource not found
500 Internal Server Error Server error

application.properties Common

# Server
server.port=8080
server.servlet.context-path=/api

# Database
spring.datasource.url=jdbc:postgresql://localhost:5432/myapp
spring.datasource.username=postgres
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=validate

# JPA
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

# Actuator
management.endpoints.web.exposure.include=health,info,metrics

# Logging
logging.level.root=INFO
logging.level.com.example=DEBUG

# File upload
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

Key Dependencies

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

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

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

<!-- Actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- H2 (Development) -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- PostgreSQL (Production) -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- Swagger/OpenAPI -->
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.3.0</version>
</dependency>

<!-- Lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

<!-- MapStruct -->
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.5.5.Final</version>
</dependency>

Key Commands

# Build
mvn clean package
mvn clean package -DskipTests

# Run
java -jar target/myapp-1.0.0.jar
mvn spring-boot:run

# Test
mvn test

# Docker
docker build -t myapp .
docker run -p 8080:8080 myapp

Project Structure

src/
├── main/
│   ├── java/com/example/myapp/
│   │   ├── MyApplication.java
│   │   ├── controller/
│   │   │   └── UserController.java
│   │   ├── service/
│   │   │   ├── UserService.java
│   │   │   └── UserServiceImpl.java
│   │   ├── repository/
│   │   │   └── UserRepository.java
│   │   ├── model/
│   │   │   ├── entity/
│   │   │   │   └── User.java
│   │   │   └── dto/
│   │   │       ├── UserRequestDTO.java
│   │   │       └── UserResponseDTO.java
│   │   ├── mapper/
│   │   │   └── UserMapper.java
│   │   ├── config/
│   │   │   └── SecurityConfig.java
│   │   └── exception/
│   │       └── GlobalExceptionHandler.java
│   └── resources/
│       ├── application.properties
│       ├── application-dev.properties
│       ├── application-prod.properties
│       └── data.sql
└── test/
    └── java/com/example/myapp/
        ├── controller/
        │   └── UserControllerTest.java
        ├── service/
        │   └── UserServiceTest.java
        └── repository/
            └── UserRepositoryTest.java

Closing

Congratulations! You've completed the Spring Boot tutorial from the basics all the way to deployment.

What you've learned:

  1. Setting up and configuring Spring Boot
  2. REST API with controllers
  3. Data persistence with JPA & Repository
  4. Service layer and DTO pattern
  5. Development database (H2) and production database (MySQL/PostgreSQL)
  6. Proper testing
  7. Monitoring with Actuator
  8. Security with Spring Security
  9. API documentation with Swagger/OpenAPI
  10. Deploying to production

Keep practicing, read the official Spring Boot documentation, and build your own projects to deepen your understanding.

Happy learning Spring Boot! 🚀