You are on page 1of 9

Annotation on Main Class

import org.springframework Indicate the main configuration class of


@SpringBoot .boot.autoconfigure a Spring Boot application. It combines
(@Configuration +
Application .SpringBootApplication;
@EnableAutoConfiguration +
@ComponentScan)
import org.springframework It is a class-level annotation. The class
@Configuration .context.annotation annotated with @Configuration used
.Configuration; by Spring Containers as a source of
bean definitions.
import org.springframework It auto-configures the bean that is
@EnableAuto .boot.autoconfigure present in the classpath and configures
it to run the methods.
Configuration .EnableAutoConfiguration;

import org.springframework It is used when we want to scan a


@Component .context.annotation package for beans. It is used with the
annotation @Configuration. We can
Scan .ComponentScan;
also specify the base packages to scan
for Spring Components.
@ComponentScan(basePackages =
"com.java")

Annotation on Bean
import It is a method-level annotation. It is
@Bean org.springframework an alternative of XML tag. It tells the
method to produce a bean to be
.context.annotation.Bean; managed by Spring Container.
Annotation on Controller Class

import It is a specific type of Spring


@RestController org.springframework.web.bind MVC controller that is used
to build RESTful web services
.annotation.RestController; it combines (@Controller +
@ResponseBody)
import Validation is commonly used
@Validated org.springframework.validation. to check user input, validate
data from external sources
annotation.Validated;
import It is used to map the web
@Request requests. It has many
org.springframework.web.bind
Mapping( value= .annotation.RequestMapping;
optional elements like
consumes, header, method,
"/uriPath") name, params, path,
produces, and value. We use
it with the class as well as the
method.

Annotation on Object
import It is used to autowire spring bean
on setter methods, instance
org.springframework.beans variable, and constructor. When
@Autowired .factory.annotation.Autowired; we use @Autowired annotation,
the spring container autowires
the bean by matching data-type.
CRUD Operation Annotation
import It maps the HTTP POST requests on the
@PostMapping org.springframework.
specific handler method. It is used to
create a web service endpoint that
(value= "/uripath”) web.bind.annotation. creates resource. It is used instead of
using: @RequestMapping(method =
PostMapping; RequestMethod.POST)
import It maps the HTTP GET requests on the
@GetMapping org.springframework.
specific handler method. It is used to
create a web service endpoint that
(value="/uriPath”) web.bind.annotation. fetches resource. It is used instead of
using: @RequestMapping(method =
GetMapping; RequestMethod.GET)
import It maps the HTTP PUT requests on the
@PutMapping org.springframework.
specific handler method. It is used to
create a web service endpoint that
(value="/uripath”) web.bind.annotation. creates or updates resource. It is used
instead of using:
PutMapping; @RequestMapping(method =
RequestMethod.PUT)
import It maps the HTTP DELETE requests on
@DeleteMapping org.springframework.
the specific handler method. It is used
to create a web service endpoint that
(value="/uripath”) web.bind.annotation. deletes a resource. It is used instead of
using: @RequestMapping(method =
DeleteMapping; RequestMethod.DELETE)
Annotation on REST request
import It is used to bind HTTP
@RequestBody org.springframework.web request with an object in a
method parameter.
.bind.annotation.RequestBody;
import used to extract values from
@PathVariable org.springframework.web.bind. the URI path and use them as
method parameters.
("email") annotation.PathVariable;
import Annotation which indicates
@RequestParam org.springframework.web.bind that a method parameter
should be bound to a
(”email”) .annotation.RequestParam; webrequest parameter.
@Pattern(regexp = "[az]" import javax.validation These annotations are used to
validate incoming data before
, message = "abc”) .constraints.*; processing it in your
@NotNull, @Min, @Max application.
import jakarta.validation.Valid; If you have a REST endpoint
@Valid then use @Valid with a DTO
class as a method parameter.

Annotation on DTO

@NotNull, import apply validation


@Email javax.validation.constraints.*;
constraints to
@Size(min = 2, fields.
max = 50)
@Pattern(regexp
= "[a-z]" ,
message = "abc”)
Annotation on Entity/Model
Configures how the primary key
@GeneratedValue import javax.persistence is generated. Common strategies
(strategy = .GenerationType; are GenerationType.IDENTITY,
GenerationType.SEQUENCE, and
GenerationType GenerationType.AUTO.
.IDENTITY)
import Marks a class as a JPA entity,
@Entity representing a table in the
javax.persistence.Entity; database.
import Specifies the name of the
database table to which the
javax.persistence.Table; entity is mapped. You can also
@Table configure other table properties
like indexes and unique
constraints using this
annotation.
import javax.persistence.Id; Specifies the primary key of the
@Id entity.
import Specifies the mapping of a field
@Column to a database column, allowing
javax.persistence.Column; you to configure properties like
(name= "abc") column name, length, and
nullable constraints.
import javax.persistence.*; Define relationships between
@OneToMany entities.

@OneToOne
import javax.persistence to be used foreign/reference
@JoinColumn key.
.JoinColumn;
(name="abc")
import Marks a field as not persistent,
@Transient meaning it won't be mapped to a
javax.persistence.Transient; database column.
Stereotype Annotation
import This annotation is a generic
stereotype for any Spring-managed
org.springframework component. It tells Spring to scan
@Component .stereotype.Component; the classpath for classes annotated
with @Component and create
beans from them.
import This annotation is used to mark a
@Controller class as a Spring MVC controller. It is
org.springframework typically used in web applications to
.stereotype.Controller; handle HTTP requests.

import It is used at class level. It tells the


@Service(value= Spring that class contains the
org.springframework business logic.
"xyzXyz") .stereotype.Service;
import It is a class-level annotation. The
repository is a DAOs (Data Access
org.springframework Object) that access the database
@Repository .stereotype.Repository; directly. The repository does all the
operations related to the database.

Transactional Annotation
import If an exception is thrown
org.springframework.transacti within the method, the
transaction can be rolled back,
@Transactional on.annotation.Transactional;
ensuring data consistency. You
can annotate a specific
method or class.
Exception Annotation
This annotation is used to define
a global exception handler that
import can be applied to multiple
org.springframework controllers. You can create a
class annotated with
.web.bind.annotation @RestControllerAdvice and
@RestControllerAdvice .RestControllerAdvice; define methods within it
annotated with
@ExceptionHandler to handle
specific exceptions.
You can use the
@ExceptionHandler annotation
@ExceptionHandler import on methods within your

(Exception.class) org.springframework.we controller classes to handle


specific exceptions for that
b.bind.annotation controller. This allows you to
.ExceptionHandler; define exception handling logic
@ExceptionHandler(Inf on a per-controller basis.

yInternException.class)
MethodArgumentNotValidException
import exception is typically thrown when
@ExceptionHandler({ org.springframework.we validating the request parameters,
request body, or method arguments
MethodArgumentNot b.bind.MethodArgument of a controller endpoint using
NotValidException; annotation-based validation (e.g.,
ValidException.class, @Valid and validation annotations
like @NotNull, @Size, etc.).
ConstraintViolation import ConstraintViolationException
exception is more closely associated
Exception.class}) javax.validation.Constrai with Bean Validation and is thrown
when there are validation
ntViolationException; constraints violated on JavaBeans.
These constraints are typically
defined using annotations like
@NotNull, @Size, @Email, etc.
import This annotation can be used to
@ResponseStatus(Http specify the HTTP status code to
Status.NOT_FOUND) org.springframework return when a specific exception
.web.bind.annotation occurs. You can use it in
combination with
.ResponseStatus; @ExceptionHandler.
Aspects Annotation
Aspects in Spring Boot are
commonly used for tasks like
import logging, security, transaction
@Aspect org.aspectj.lang management, and
performance monitoring.
.annotation.Aspect; They allow you to keep these
cross-cutting concerns
separate from your core
business logic, making your
codebase more modular and
maintainable.
You define pointcut
@AfterThrowing expressions using annotations
import like @Before, @After,
@Before, org.aspectj.lang @Around, etc. These
expressions determine where
@After, .annotation.*; in your codebase the advice
methods should be applied.
@Around

You might also like