DEV Community

Cover image for # Spring Boot Annotation Cheat Sheet
Bellamer
Bellamer

Posted on

# Spring Boot Annotation Cheat Sheet

Spring Boot annotations are powerful tools to simplify development, configuration, and security. Here's a comprehensive cheat sheet of common annotations, what they do, and when to use them.


Core Annotations

@SpringBootApplication

What it does:

Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan into a single annotation.

When to use:

Use on the main class to indicate it's the entry point for the Spring Boot application.


@Component

What it does:

Marks a class as a Spring-managed component.

When to use:

Use for general-purpose Spring-managed components not covered by @Service, @Repository, or @Controller.


@Service

What it does:

Marks a class as a service layer component.

When to use:

Use to define business logic in the service layer.


@Repository

What it does:

Indicates that a class is a data access object (DAO).

When to use:

Use in the persistence layer to interact with the database.


@Configuration

What it does:

Indicates that a class contains Spring configuration.

When to use:

Use to define bean configurations and customize the Spring context.


@Bean

What it does:

Marks a method as a producer of a Spring-managed bean.

When to use:

Use inside @Configuration-annotated classes to define beans.


@Primary

What it does:

Marks a bean as the primary one when multiple beans of the same type exist.

When to use:

Use when there are multiple beans of the same type, and you want to specify the default.


@Autowired

What it does:

Injects a dependency into a Spring-managed bean.

When to use:

Use for dependency injection in Spring-managed components.


@Qualifier

What it does:

Used to specify which bean to inject when multiple candidates exist.

When to use:

Use with @Autowired to resolve ambiguity between multiple beans.


Web Layer Annotations

@RestController

What it does:

A combination of @Controller and @ResponseBody, making the class serve RESTful web services.

When to use:

Use on classes where you want to define REST endpoints.


@Controller

What it does:

Marks a class as a Spring MVC controller.

When to use:

Use when building web applications and handling HTTP requests.


@RequestMapping

What it does:

Maps HTTP requests to handler methods of @Controller or @RestController.

When to use:

Use to define endpoint mappings for controllers.


@GetMapping

What it does:

Maps HTTP GET requests to handler methods.

When to use:

Use for read-only operations like fetching data.


@PostMapping

What it does:

Maps HTTP POST requests to handler methods.

When to use:

Use for creating or submitting data.


@PutMapping

What it does:

Maps HTTP PUT requests to handler methods.

When to use:

Use for updating existing resources.


@DeleteMapping

What it does:

Maps HTTP DELETE requests to handler methods.

When to use:

Use for deleting resources.


@PatchMapping

What it does:

Maps HTTP PATCH requests to handler methods.

When to use:

Use for partial updates to resources.


@PathVariable

What it does:

Extracts a value from the URL path.

When to use:

Use when you want to pass URL path parameters to a method.


@RequestParam

What it does:

Extracts query parameters from the request.

When to use:

Use when you want to extract query parameters from the URL.


@RequestBody

What it does:

Maps the body of the HTTP request to a Java object.

When to use:

Use for binding request data (e.g., JSON) to method parameters.


@ResponseBody

What it does:

Indicates that the return value of a method should be bound to the HTTP response body.

When to use:

Use in non-@RestController classes to return JSON or other formats.


@ResponseStatus

What it does:

Marks a method or exception class with an HTTP status code.

When to use:

Use to set custom HTTP response codes.


@ExceptionHandler

What it does:

Handles exceptions thrown in controller methods.

When to use:

Use in controller classes to define custom error handling.


@CrossOrigin

What it does:

Enables Cross-Origin Resource Sharing (CORS) for a specific controller or method.

When to use:

Use to allow cross-origin requests to your API.


Security Annotations

@EnableWebSecurity

What it does:

Enables Spring Security's web security features.

When to use:

Use on a configuration class to define authentication and authorization rules.


@Secured

What it does:

Restricts access to methods based on roles.

When to use:

Use to allow access only to users with specific roles.


@PreAuthorize

What it does:

Applies more complex access control expressions before a method is called.

When to use:

Use when access control requires conditions beyond roles, such as checking user permissions or attributes.


@PostAuthorize

What it does:

Applies access control expressions after a method is called.

When to use:

Use when access control depends on the result of the method.


@RolesAllowed

What it does:

Specifies allowed roles for access, as defined by JSR-250 standards.

When to use:

Use as an alternative to @Secured for role-based access control.


@EnableGlobalMethodSecurity

What it does:

Enables method-level security annotations like @Secured, @PreAuthorize, and @PostAuthorize.

When to use:

Use in security configuration to activate method-level security.


@WithMockUser

What it does:

Sets up a mock authenticated user for testing purposes.

When to use:

Use in test cases to simulate authenticated users.


Persistence Layer Annotations

@Entity

What it does:

Marks a class as a JPA entity.

When to use:

Use to map a class to a database table.


@Table

What it does:

Customizes the table name and other table properties in a JPA entity.

When to use:

Use with @Entity to customize the table mapping.


@Id

What it does:

Marks a field as the primary key in a JPA entity.

When to use:

Use to define the primary key field of an entity.


@GeneratedValue

What it does:

Specifies how the primary key value is generated.

When to use:

Use with @Id to define the generation strategy (e.g., AUTO, IDENTITY).


@Column

What it does:

Customizes column mapping for a field in a JPA entity.

When to use:

Use to specify column properties like name, length, etc.


@Transactional

What it does:

Defines transactional boundaries for methods.

When to use:

Use on methods or classes to handle transactions automatically.


This cheat sheet covers the essential Spring Boot annotations, including core, web, security, and persistence layers. Master these, and you'll write cleaner, more secure, and efficient Spring applications. Happy coding!

Top comments (0)