1. What is the main purpose of the @RequestMapping annotation in Spring MVC?
Why:@RequestMapping is used to map request URLs to specific controllers or handler methods.
2. Where can the @RequestMapping annotation be applied?
Why:@RequestMapping can be used on both class level and method level because its @Target includes TYPE and METHOD.
3. What happens if a controller class has @RequestMapping but a handler method inside it does not?
Why: Handler methods without @RequestMapping are not mapped, even if the enclosing class has @Controller and a valid @RequestMapping.
4. What does the method element of @RequestMapping specify?
Why: The method element defines the HTTP request methods to map to, narrowing the primary mapping.
5. How do you specify that a handler method should support both GET and DELETE HTTP methods using @RequestMapping?
@RequestMapping(value = "/{id}", /* add method element */)
Why: You can specify multiple HTTP methods by using an array of RequestMethod values, such as {RequestMethod.GET, RequestMethod.DELETE}.
6. What is the purpose of the params element in @RequestMapping?
Why: The params element is used to narrow the primary mapping by query string parameters, so the handler is only invoked when the query string matches.
7. Which @RequestMapping configuration will match any request that has a query parameter named dept, regardless of its value?
@RequestMapping(/* specify params element */)
Why: You can skip the value part of the parameter; in that case, all requests having the specified parameter name will be mapped regardless of their values.
8. What does the headers element of @RequestMapping do?
Why: The headers element is similar to params but is used to specify HTTP header key-value pairs to uniquely define handler methods.
9. What does the consumes element of @RequestMapping specify?
Why: The consumes element defines the consumable media types of the mapped request, narrowing the primary mapping based on the request's Content-Type.
10. What does the produces element of @RequestMapping specify?
Why: The produces element defines the producible media types of the mapped request, narrowing the primary mapping based on the Accept header.
11. What is the purpose of the path element in @RequestMapping?
Why:path is an alias for value and is used to specify the path mapping URIs, such as "/profile".
12. How do you define a URI template variable {userId} in the path of a handler method?
@RequestMapping(/* specify value element */)
Why: URI templates use curly braces, such as /{userId}, to define a variable part of the path that can be captured later.
13. Which annotation is used to capture a URI template variable in a handler method parameter?
Why: URI templates can be captured by a handler's method parameter annotated with @PathVariable.
14. What does an empty @RequestMapping (without a path) on a method do?
Why: An empty @RequestMapping (without path) acts as a fallback and will map to all URLs for which specific matches are not available.
15. What is the name element of @RequestMapping used for?
Why: The name element is used to assign a name to the mapping, which can be useful for identification or tooling.
16. What is the version element introduced in Spring 7's @RequestMapping used for?
Why: Spring 7 introduces a native version element to narrow the primary mapping by an API version, supporting declarative API versioning.
17. What is an Ant-style path pattern in the context of @RequestMapping?
Why: Ant-style path patterns support wildcards such as ** to match any number of path segments, for example /**/users matches any URL ending with users.
18. How can you negate a media type in the consumes or produces element of @RequestMapping?
Why: Expressions can be negated by using the "!" operator, as in "!text/plain", which matches all requests with a Content-Type or Accept header other than "text/plain".