Q1. What is @RequestMapping and its attributes?
Attributes:
•
•
•
•
•
•
It can be used at class and method level.
@RequestMapping is used to map web requests to handler methods.Attributes:
•
value (URL)•
method (HTTP method)•
params (filter by request parameters)•
headers (filter by headers)•
consumes (media types consumed)•
produces (media types produced)It can be used at class and method level.
Q2. What are the shortcut annotations for @RequestMapping?
Spring provides
They are composed annotations that specify the HTTP method.
They reduce verbosity and improve readability.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping.They are composed annotations that specify the HTTP method.
They reduce verbosity and improve readability.
Q3. How do you handle path variables in request mapping?
Use
Example:
@PathVariable annotation to bind a method parameter to a URI template variable.Example:
@GetMapping("/users/{id}")
public String getUser(@PathVariable("id") Long id) { ... }
Q4. How do you handle query parameters?
Use
Example:
@RequestParam to bind request parameters to method arguments.Example:
@GetMapping("/users")
public String listUsers(@RequestParam(defaultValue="1") int page) { ... }
Q5. What is matrix variables support in Spring MVC?
Matrix variables allow multiple parameters in a path segment, separated by semicolons.
Spring MVC supports them via
They need to be enabled in the configuration.
Example:
Spring MVC supports them via
@MatrixVariable.They need to be enabled in the configuration.
Example:
/users/42;q=5;sort=name