0% found this document useful (0 votes)
34 views

SpringBoot Annotation

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

SpringBoot Annotation

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
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 org.springframework
application. It combines (@Configuration +
Application .boot.autoconfigure
@EnableAutoConfiguration + @ComponentScan)
.SpringBootApplication;

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

import
@EnableAuto org.springframework It auto-configures the bean that is present in the
Configuration .boot.autoconfigure 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 org.springframework
also specify the base packages to scan for Spring
Scan .context.annotation
Components.
.ComponentScan;
@ComponentScan(basePackages = "com.java")

Annotation on Bean

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


@Bean org.springframework XML <bean> tag. It tells the method to produce a bean
.context.annotation.Bean; to be managed by Spring Container.
Annotation on Controller Class @techwithvishalraj

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


@RestContro
org.springframework.web.bind used to build RESTful web services
ller
.annotation.RestController; it combines (@Controller + @ResponseBody)

import
Validation is commonly used to check user input,
@Validated org.springframework.validation.
validate data from external sources
annotation.Validated;

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


import
Mapping( optional elements like consumes, header, method,
org.springframework.web.bind
value= name, params, path, produces, and value. We use it
.annotation.RequestMapping;
"/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 org.springframework.beans
@Autowired annotation, the spring container auto-
.factory.annotation.Autowired;
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 org.springframework.
endpoint that creates resource.
(value= "/uripath”) web.bind.annotation.
It is used instead of using:
GetMapping;
@RequestMapping(method = RequestMethod.POST)

It maps the HTTP GET requests on the specific


import
handler method. It is used to create a web service
@GetMapping org.springframework.
endpoint that fetches resource.
(value="/uriPath”) web.bind.annotation.
It is used instead of using:
GetMapping;
@RequestMapping(method = RequestMethod.GET)

It maps the HTTP PUT requests on the specific


import
handler method. It is used to create a web service
@PutMapping org.springframework.
endpoint that creates or updates resource.
(value="/uripath”) web.bind.annotation.
It is used instead of using:
PutMapping;
@RequestMapping(method = RequestMethod.PUT)

It maps the HTTP DELETE requests on the specific


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

import
It is used to bind HTTP request with an
@RequestBody org.springframework.web
object in a method parameter.
.bind.annotation.RequestBody;

@PathVariable import
used to extract values from the URI path
("email") org.springframework.web.bind.
and use them as method parameters
annotation.PathVariable;

import Annotation which indicates that a method


@RequestParam
org.springframework.web.bind parameter should be bound to a
(”email”)
.annotation.RequestParam; webrequest parameter.

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


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

If you have a REST endpoint then use


import jakarta.validation.Valid;
@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]", javax.validation.constraints.*;
message = "abc”)
Annotation on Entity/Model @techwithvishalraj

@GeneratedValue Configures how the primary key is generated.


import
(strategy = Common strategies are GenerationType.IDENTITY,
javax.persistence
GenerationType GenerationType.SEQUENCE, and
.GenerationType;
.IDENTITY) GenerationType.AUTO.

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


@Entity
javax.persistence.Entity; in the database.

Specifies the name of the database table to which


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

import
@Id Specifies the primary key of the entity.
javax.persistence.Id;

Specifies the mapping of a field to a database


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

@OneToMany
import javax.persistence.*; Define relationships between entities.
@OneToOne

@JoinColumn import javax.persistence


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

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


@Transient
javax.persistence.Transien; 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 org.springframework
scan the classpath for classes annotated with
.stereotype.Component;
@Component and create beans from them.

import This annotation is used to mark a class as a


@Controller org.springframework Spring MVC controller. It is typically used in
.stereotype.Controller; web applications to handle HTTP requests.

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

It is a class-level annotation. The repository is


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

Transactional Annotation

If an exception is thrown within the method,


import
the transaction can be rolled back, ensuring
@Transactional org.springframework.transacti
data consistency.
on.annotation.Transactional;
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
org.springframework controllers. You can create a class annotated
@RestControllerAdvice
.web.bind.annotation with @RestControllerAdvice and define methods
.RestControllerAdvice; within it annotated with @ExceptionHandler to
handle specific exceptions.

@ExceptionHandler You can use the @ExceptionHandler annotation


import
(Exception.class) on methods within your controller classes to
org.springframework.we
handle specific exceptions for that controller.
b.bind.annotation
@ExceptionHandler(Inf This allows you to define exception handling logic
.ExceptionHandler;
yInternException.class) on a per-controller basis.

MethodArgumentNotValidException exception is
typically thrown when validating the request
import parameters, request body, or method arguments
org.springframework.we of a controller endpoint using annotation-based
@ExceptionHandler({
b.bind.MethodArgument validation (e.g., @Valid and validation
MethodArgumentNot
NotValidException; annotations like @NotNull, @Size, etc.).
ValidException.class,
ConstraintViolationException exception is more
ConstraintViolation
import closely associated with Bean Validation and is
Exception.class})
javax.validation.Constrai 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 org.springframework status code to return when a specific exception
Status.NOT_FOUND) .web.bind.annotation 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 org.aspectj.lang monitoring. They allow you to keep these cross-cutting
.annotation.Aspect; 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
org.aspectj.lang
@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
org.springframework.boot
webEnvironment = it with the webEnvironment attribute to specify the
.test.context
WebEnvironment. type of web environment( WebEnvironment.MOCK
.SpringBootTest;
MOCK) for a mock servlet environment).

It's often used to replace real dependencies with


@Mock import org.mockito.Mock;
mock objects to isolate the class under test.

import it is about the automatic injection of those mock


@InjectMocks
org.mockito.InjectMocks; objects into the class or object you are testing.

import This is a standard JUnit annotation used to mark a


@Test
org.junit.jupiter.api.Test; method as a test method.
Thank
you!

EXPLORE MORE

@techwithvishalraj

You might also like