Spring Annotations
Spring Annotations
Annotations
LAHIRU
LIYANAPATHIRANA
PAGE 2/29
LAHIRU
LIYANAPATHIRANA
PAGE 3/29
@Controller
Classes annotated with the @Controller,
handle HTTP requests and return
responses. They can contain methods that
process input and return view names with
model data through ModelAndView
objects or String view names.
LAHIRU
LIYANAPATHIRANA
PAGE 4/29
@ResponseBody
Indicates that the method returns the data
directly to the HTTP response body,
bypassing the view resolution. This is used
in RESTful APIs and can automatically
serialize objects to JSON/XML.
LAHIRU
LIYANAPATHIRANA
PAGE 5/29
@RestController
It is a specialized version of the
@Controller that combines @Controller and
@ResponseBody. It is specifically designed
for RESTful web services where every
method returns data rather than a view.
LAHIRU
LIYANAPATHIRANA
PAGE 6/29
@ResponseStatus
Marks method or exception class with
HTTP status code and reason. It is possible
to provide a custom reason message.
LAHIRU
LIYANAPATHIRANA
PAGE 7/29
@RequestMapping
Maps incoming HTTP requests to specific
handler classes or handler methods within
a controller.
LAHIRU
LIYANAPATHIRANA
PAGE 8/29
@RequestMapping
produces: Response Content-Type (e.g.,
produces= "application/json")
Path variables (e.g., "/products/{id}")
LAHIRU
LIYANAPATHIRANA
PAGE 9/29
@GetMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.GET).
LAHIRU
LIYANAPATHIRANA
PAGE 10/29
@PostMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.POST).
LAHIRU
LIYANAPATHIRANA
PAGE 11/29
@PutMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.PUT).
LAHIRU
LIYANAPATHIRANA
PAGE 12/29
@DeleteMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.DELETE).
LAHIRU
LIYANAPATHIRANA
PAGE 13/29
@PatchMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.PATCH).
LAHIRU
LIYANAPATHIRANA
PAGE 14/29
@RequestParam
Binds request parameters to the method
parameters and extracts query
parameters from the URL. It can handle
multiple parameter types.
It supports the following attributes:
required: Specifies if the parameter is
mandatory
defaultValue: Default value if the
parameter is missing
value/name: Parameter name
LAHIRU
LIYANAPATHIRANA
PAGE 15/29
@PathVariable
Extracts values from the URL template
variables, and binds them to the method
parameters. In the URL ("/products/{id}"),
“id” can be extracted to a method
parameter. It can be used to make
dynamic URLs.
LAHIRU
LIYANAPATHIRANA
PAGE 16/29
@RequestBody
Binds HTTP request body to method
parameter and automatically deserialize
JSON/XML. It is used in POST or PUT
requests to read data sent by the client
and supports content negotiations.
LAHIRU
LIYANAPATHIRANA
PAGE 17/29
@RequestHeader
Binds HTTP request header to method
parameter and it is useful when extracting
specific headers. Request headers can be
marked as required or optional and
support default values.
LAHIRU
LIYANAPATHIRANA
PAGE 18/29
@ModelAttribute
Binds a method parameter or method
return value to a named model attribute
and automatically populates the object
with data from the form submissions.
LAHIRU
LIYANAPATHIRANA
PAGE 19/29
@SessionAttributes
Specifies attributes that are stored in the
session, which enables maintaining the
state between requests. It is used for
multi-step forms and session-bound tasks.
LAHIRU
LIYANAPATHIRANA
PAGE 20/29
@CrossOrigin
Enables cross-origin requests and can be
applied at the class or method level.
It supports the following attributes:
origins: Allowed origins
methods: Allowed methods
allowedHeaders: Allowed headers
exposedHeaders: Exposed headers
allowCredentials: Allows credentials
LAHIRU
LIYANAPATHIRANA
PAGE 21/29
@CookieValue
Binds cookie values to handler method
parameters.
It supports the following attributes:
required: Specifies if the cookie is
mandatory
defaultValue: Default value if the
cookie is missing
name/value: Cookie name
LAHIRU
LIYANAPATHIRANA
PAGE 22/29
@Valid
Triggers validation of an object on the
method level. It is used with the
BindingResult interface to capture
validation errors.
This is used in conjunction with the
following validation annotations and
custom validations:
@NotNull: Ensures field is not null
@NotEmpty: Ensures collection/string is
not empty
@NotBlank: Ensures string is not
null/empty/whitespace
@Size: Validates collection size or string
length
@Min/@Max: Validates numeric bounds
@Pattern: Validates string against regex
@Email: Validates email format
LAHIRU
LIYANAPATHIRANA
PAGE 23/29
@Valid
@Positive/@PositiveOrZero: Validates
numbers
@Future/@Past: Validates dates
@Range: Validates numeric ranges
LAHIRU
LIYANAPATHIRANA
PAGE 24/29
@Validated
Marks class for Spring validation, and
enabled validation groups.
LAHIRU
LIYANAPATHIRANA
PAGE 25/29
@Validated
Defines a global exception handler for all
controllers. It is used for centralized
exception handling.
It supports:
basePackages: Limits scanning to
specific packages
annotations: Limits to controllers with
specific annotations
assignableTypes: Limits to specific
controller types
LAHIRU
LIYANAPATHIRANA
PAGE 26/29
@RestControllerAdvice
It is a specialized version of
@ControllerAdvice that combines
@ControllerAdvice and @ResponseBody.
It is used for global RESTful exception
handling.
LAHIRU
LIYANAPATHIRANA
PAGE 27/29
@ExceptionHandler
Handles exceptions thrown by controller
methods. It can be configured to handle
specific exceptions and custom error
responses.
LAHIRU
LIYANAPATHIRANA
PAGE 28/29
@InitBinder
Registers custom editors for data binding,
validation, type conversions, and pre-
processing during binding.
LAHIRU
LIYANAPATHIRANA
PAGE 7/7
LAHIRU
LIYANAPATHIRANA