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

Spring Annotations

Spring MVC is a web framework that facilitates web application development through a variety of annotations that define behaviors and configurations. Key annotations include @Controller for handling requests, @RestController for RESTful services, and various mapping annotations like @GetMapping and @PostMapping for specific HTTP methods. Additional annotations support request handling, validation, session management, and exception handling, enhancing the framework's capabilities.

Uploaded by

Akshay Walunj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Spring Annotations

Spring MVC is a web framework that facilitates web application development through a variety of annotations that define behaviors and configurations. Key annotations include @Controller for handling requests, @RestController for RESTful services, and various mapping annotations like @GetMapping and @PostMapping for specific HTTP methods. Additional annotations support request handling, validation, session management, and exception handling, enhancing the framework's capabilities.

Uploaded by

Akshay Walunj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Spring MVC

Annotations

LAHIRU
LIYANAPATHIRANA
PAGE 2/29

What is Spring MVC?


Spring MVC is a powerful web
framework that is part of the Spring
ecosystem.

Spring MVC simplifies web development


with its separation of concern and robust
and scalable capabilities.

Spring MVC provides a variety of


annotations that simplify the development
of web applications by allowing
developers to define behavior and
configurations directly in their code.

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.

It supports multiple attributes:


value/path: URL pattern (e.g., "/users",
"/products/{id}")
method: HTTP method (GET, POST, PUT,
DELETE, etc.)
params: Request parameters
headers: Request headers (e.g., headers =
"content-type=text/*")
consumes: Content-Type (e.g., consumes =
"application/json")

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).

It maps HTTP GET requests to specific


handler methods and is ideally used for
read/retrieve operations. Supports all
@RequestMapping attributes.

LAHIRU
LIYANAPATHIRANA
PAGE 10/29

@PostMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.POST).

It maps HTTP POST requests to specific


handler methods and is ideally used to
create new resources and form
submissions.

LAHIRU
LIYANAPATHIRANA
PAGE 11/29

@PutMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.PUT).

It maps HTTP PUT requests to specific


handler methods and is ideally used to
update existing resources.

LAHIRU
LIYANAPATHIRANA
PAGE 12/29

@DeleteMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.DELETE).

It maps HTTP DELETE requests to specific


handler methods and is ideally used to
remove resources.

LAHIRU
LIYANAPATHIRANA
PAGE 13/29

@PatchMapping
It is a shortcut for @RequestMapping(method
= RequestMethod.PATCH).

It maps HTTP PATCH requests to specific


handler methods and is ideally used to
update and process resources partially.

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

Did You Find This


Post Useful?

Stay Tuned for More


Spring Related Posts
Like This

LAHIRU
LIYANAPATHIRANA

You might also like