0% found this document useful (0 votes)
214 views10 pages

Spring Boot Annotations Guide

The document discusses various Spring Boot annotations used for configuration, controllers, CRUD operations, REST requests, data transfer objects, and entities. Key annotations include @SpringBootApplication, @Configuration, @ComponentScan, @RestController, @RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @RequestBody, @PathVariable, @RequestParam, @Valid, @Entity, @Table, and JPA annotations like @Id, @GeneratedValue. These annotations simplify configuration and mapping of Spring Boot applications, controllers, requests, and entities.

Uploaded by

Sokaina Daabal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views10 pages

Spring Boot Annotations Guide

The document discusses various Spring Boot annotations used for configuration, controllers, CRUD operations, REST requests, data transfer objects, and entities. Key annotations include @SpringBootApplication, @Configuration, @ComponentScan, @RestController, @RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @RequestBody, @PathVariable, @RequestParam, @Valid, @Entity, @Table, and JPA annotations like @Id, @GeneratedValue. These annotations simplify configuration and mapping of Spring Boot applications, controllers, requests, and entities.

Uploaded by

Sokaina Daabal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

@techwithvishalraj

Java
SpringBoot Annotation
Annotation on Main Class @techwithvishalraj

import
Indicate the main configuration class of a Spring Boot
@SpringBoot [Link]
application. It combines (@Configuration +
Application .[Link]
@EnableAutoConfiguration + @ComponentScan)
.SpringBootApplication;

import
It is a class-level annotation. The class annotated
[Link]
@Configuration with @Configuration used by Spring Containers as a
.[Link]
source of bean definitions.
.Configuration;

import
@EnableAuto [Link] It auto-configures the bean that is present in the
Configuration .[Link] classpath and configures it to run the methods.
.EnableAutoConfiguration;

It is used when we want to scan a package for beans.


import
It is used with the annotation @Configuration. We can
@Component [Link]
also specify the base packages to scan for Spring
Scan .[Link]
Components.
.ComponentScan;
@ComponentScan(basePackages = "[Link]")

Annotation on Bean

import It is a method-level annotation. It is an alternative of


@Bean [Link] XML <bean> tag. It tells the method to produce a bean
.[Link]; to be managed by Spring Container.
Annotation on Controller Class @techwithvishalraj

import It is a specific type of Spring MVC controller that is


@RestContro
[Link] used to build RESTful web services
ller
.[Link]; it combines (@Controller + @ResponseBody)

import
Validation is commonly used to check user input,
@Validated [Link].
validate data from external sources
[Link];

@Request It is used to map the web requests. It has many


import
Mapping( optional elements like consumes, header, method,
[Link]
value= name, params, path, produces, and value. We use it
.[Link];
"/uriPath") with the class as well as the method.

Annotation on Object

It is used to autowire spring bean on setter methods,


import
instance variable, and constructor. When we use
@Autowired [Link]
@Autowired annotation, the spring container auto-
.[Link];
wires the bean by matching data-type.
@techwithvishalraj

CRUD Operation Annotation

It maps the HTTP POST requests on the specific


import
handler method. It is used to create a web service
@PostMapping [Link].
endpoint that creates resource.
(value= "/uripath”) [Link].
It is used instead of using:
GetMapping;
@RequestMapping(method = [Link])

It maps the HTTP GET requests on the specific


import
handler method. It is used to create a web service
@GetMapping [Link].
endpoint that fetches resource.
(value="/uriPath”) [Link].
It is used instead of using:
GetMapping;
@RequestMapping(method = [Link])

It maps the HTTP PUT requests on the specific


import
handler method. It is used to create a web service
@PutMapping [Link].
endpoint that creates or updates resource.
(value="/uripath”) [Link].
It is used instead of using:
PutMapping;
@RequestMapping(method = [Link])

It maps the HTTP DELETE requests on the specific


import handler method. It is used to create a web service
@DeleteMapping [Link]. endpoint that deletes a resource.
(value="/uripath”) [Link]. It is used instead of using:
DeleteMapping; @RequestMapping(method =
[Link])
Annotation on REST request @techwithvishalraj

import
It is used to bind HTTP request with an
@RequestBody [Link]
object in a method parameter.
.[Link];

@PathVariable import
used to extract values from the URI path
("email") [Link].
and use them as method parameters
[Link];

import Annotation which indicates that a method


@RequestParam
[Link] parameter should be bound to a
(”email”)
.[Link]; webrequest parameter.

@Pattern(regexp = "[a- import These annotations are used to validate


z]", message = "abc”) [Link] incoming data before processing it in your
@NotNull, @Min, @Max .constraints.*; application.

If you have a REST endpoint then use


import [Link];
@Valid @Valid with a DTO class as a method
parameter.

Annotation on DTO
@NotNull, @Email
@Size(min = 2, max = 50) import
apply validation constraints to fields.
@Pattern(regexp = "[a-z]", [Link].*;
message = "abc”)
Annotation on Entity/Model @techwithvishalraj

@GeneratedValue Configures how the primary key is generated.


import
(strategy = Common strategies are [Link],
[Link]
GenerationType [Link], and
.GenerationType;
.IDENTITY) [Link].

import Marks a class as a JPA entity, representing a table


@Entity
[Link]; in the database.

Specifies the name of the database table to which


import the entity is mapped. You can also configure other
@Table
[Link]; table properties like indexes and unique
constraints using this annotation.

import
@Id Specifies the primary key of the entity.
[Link];

Specifies the mapping of a field to a database


@Column(name= import
column, allowing you to configure properties like
"abc") [Link];
column name, length, and nullable constraints.

@OneToMany
import [Link].*; Define relationships between entities.
@OneToOne

@JoinColumn import [Link]


to be used foreign/reference key
(name="abc") .JoinColumn;

import Marks a field as not persistent, meaning it won't be


@Transient
[Link]; mapped to a database column.
Stereotype Annotation @techwithvishalraj

This annotation is a generic stereotype for any


import
Spring-managed component. It tells Spring to
@Component [Link]
scan the classpath for classes annotated with
.[Link];
@Component and create beans from them.

import This annotation is used to mark a class as a


@Controller [Link] Spring MVC controller. It is typically used in
.[Link]; web applications to handle HTTP requests.

@Service(value= import [Link] It is used at class level. It tells the Spring that
"xyzXyz") .[Link]; class contains the business logic.

It is a class-level annotation. The repository is


import
a DAOs (Data Access Object) that access the
@Repository [Link]
database directly. The repository does all the
.[Link];
operations related to the database.

Transactional Annotation

If an exception is thrown within the method,


import
the transaction can be rolled back, ensuring
@Transactional [Link]
data consistency.
[Link];
You can annotate a specific method or class.
Exception Annotation @techwithvishalraj

This annotation is used to define a global


import exception handler that can be applied to multiple
[Link] controllers. You can create a class annotated
@RestControllerAdvice
.[Link] with @RestControllerAdvice and define methods
.RestControllerAdvice; within it annotated with @ExceptionHandler to
handle specific exceptions.

@ExceptionHandler You can use the @ExceptionHandler annotation


import
([Link]) on methods within your controller classes to
[Link]
handle specific exceptions for that controller.
[Link]
@ExceptionHandler(Inf This allows you to define exception handling logic
.ExceptionHandler;
[Link]) on a per-controller basis.

MethodArgumentNotValidException exception is
typically thrown when validating the request
import parameters, request body, or method arguments
[Link] of a controller endpoint using annotation-based
@ExceptionHandler({
[Link] validation (e.g., @Valid and validation
MethodArgumentNot
NotValidException; annotations like @NotNull, @Size, etc.).
[Link],
ConstraintViolationException exception is more
ConstraintViolation
import closely associated with Bean Validation and is
[Link]})
[Link] thrown when there are validation constraints
ntViolationException; violated on JavaBeans. These constraints are
typically defined using annotations like
@NotNull, @Size, @Email, etc.

import This annotation can be used to specify the HTTP


@ResponseStatus(Http [Link] status code to return when a specific exception
Status.NOT_FOUND) .[Link] occurs. You can use it in combination with
.ResponseStatus; @ExceptionHandler.
Aspects Annotation @techwithvishalraj

Aspects in Spring Boot are commonly used for tasks like


import logging, security, transaction management, and performance
@Aspect [Link] monitoring. They allow you to keep these cross-cutting
.[Link]; concerns separate from your core business logic, making your
codebase more modular and maintainable.

@AfterThrowing You define pointcut expressions using annotations like


import
@Before, @Before, @After, @Around, etc. These expressions determine
[Link]
@After, where in your codebase the advice methods should be
.annotation.*;
@Around applied.

Test Annotation

@SpringBootTest It loads the entire Spring application context,


import
@SpringBootTest( making it suitable for integration tests. You can use
[Link]
webEnvironment = it with the webEnvironment attribute to specify the
.[Link]
WebEnvironment. type of web environment( [Link]
.SpringBootTest;
MOCK) for a mock servlet environment).

It's often used to replace real dependencies with


@Mock import [Link];
mock objects to isolate the class under test.

import it is about the automatic injection of those mock


@InjectMocks
[Link]; objects into the class or object you are testing.

import This is a standard JUnit annotation used to mark a


@Test
[Link]; method as a test method.
Thank
you!

EXPLORE MORE

@techwithvishalraj

You might also like