Last updated
Was this helpful?
Last updated
Was this helpful?
Spring Boot provides a set of annotations for defining controllers in web applications, handling HTTP requests, and configuring response behaviors. These annotations are used to build RESTful APIs or traditional MVC applications.
These annotations define the controller layer in a Spring Boot application.
@Controller
Marks a class as a Spring MVC controller.
Used in Spring MVC applications that return HTML views.
@RestController
Specialized version of @Controller
that combines @Controller
and @ResponseBody
.
Used in RESTful APIs where the response is JSON or XML instead of HTML.
@RestController = @Controller + @ResponseBody
These annotations map HTTP requests to controller methods.
@RequestMapping
Maps an HTTP request to a method or class.
Supports various attributes like value
, method
, produces
, consumes
.
Class-level @RequestMapping
prefixes all method mappings.
Example (Class-level and Method-level Mapping):
@RequestMapping
Spring Boot provides specific annotations for each HTTP method:
@GetMapping
Maps HTTP GET requests.
@PostMapping
Maps HTTP POST requests (used for creating resources).
@RequestBody
is used to extract JSON request payload.
@PutMapping
Maps HTTP PUT requests (used for updating resources).
@DeleteMapping
Maps HTTP DELETE requests.
Example:
@PatchMapping
Maps HTTP PATCH requests (used for partial updates).
Example:
@GetMapping
Handles HTTP GET requests
@PostMapping
Handles HTTP POST requests
@PutMapping
Handles HTTP PUT requests
@DeleteMapping
Handles HTTP DELETE requests
@PatchMapping
Handles HTTP PATCH requests