1. What is the main purpose of the @PathVariable annotation in Spring MVC?
Why:@PathVariable is used to capture the value of a URI template variable and bind it to a method parameter.
2. Where can the @PathVariable annotation be applied?
Why:@PathVariable is designed to be used on a handler method parameter to capture a URI template variable.
3. Which element of @PathVariable is used to specify the URI template variable name?
Why: You can use the value or name element of @PathVariable to define the URI template variable name.
4. Can you skip the variable name in @PathVariable if the URI template variable name matches the method parameter name?
Why: If the URI template variable name matches the method parameter name, you can omit the @PathVariable value. This works when the code is compiled with debugging information.
5. Can a single handler method have multiple @PathVariable annotations?
Why: A handler method can declare multiple @PathVariable parameters to capture multiple URI template variables.
6. What happens if you define two handler methods with different template variable names but the same base URI?
Why: Different template variable names do not define different URI paths, so Spring cannot distinguish the mappings and throws an ambiguous mapping exception.
7. How can you make a path variable optional in Spring MVC?
Why: Setting required = false on @PathVariable makes the path variable optional.
8. When a path variable is optional and the segment is missing from the URL, what value does the parameter receive?
Why: If required = false and the path variable is absent, the parameter is set to null.
9. What type should you use for an optional path variable to avoid problems with null values?
Why: When using required = false, you should use a nullable wrapper type like Long instead of a primitive like long, because primitives cannot hold null.
10. What is the default value of the required element in @PathVariable?
Why: By default, path variables are required, meaning required defaults to true.
11. What happens if a required path variable is missing from the URL?
Why: When a path variable is required and missing from the URL, Spring will throw an exception.
12. Can you use a Map as a method parameter to capture multiple path variables?
Why: If the method parameter is a Map annotated with @PathVariable, it will contain all URI template variable names and values.
13. Can a template URI be specified at the controller class level using @RequestMapping?
Why: Template URIs can be specified in @RequestMapping at the controller class level as well as at the method level.
14. How can you restrict a path variable to match only letters (e.g., [a-zA-Z]+)?
Why: You can embed a regular expression in the URI template, such as {name:[a-zA-Z]+}, to restrict the path variable pattern.
15. What is the annotation target of @PathVariable?
Why:@PathVariable is defined with @Target(ElementType.PARAMETER), meaning it can only be applied to method parameters.
16. What is the retention policy of @PathVariable?
Why:@PathVariable has @Retention(RetentionPolicy.RUNTIME), so it is available at runtime for Spring to process.
17. In which package is @PathVariable located?
Why:@PathVariable is part of the org.springframework.web.bind.annotation package.
18. Which code snippet correctly captures a path variable named id?
@RequestMapping("/users/{id}")
public String getUser(/* missing parameter */) { ... }
Why: You use @PathVariable("id") to bind the URI template variable id to the method parameter.
19. Which annotation correctly marks a method parameter as an optional path variable?
public String getProfile(/* missing annotation */ Long userId) { ... }
Why: You set required = false in @PathVariable to make a path variable optional.
20. Which URI template correctly restricts the path variable to only letters using a regular expression?
@RequestMapping(/* missing URI template */)
public String getByName(@PathVariable("name") String name) { ... }
Why: The URI template /users/{name:[a-zA-Z]+} uses a regular expression to match only letter-based values for the path variable.