Spring MVC - Automatic Parameter Names Discovery MCQ
0/13
0✓
0✗
1. What is the main purpose of ParameterNameDiscoverer in Spring MVC?
Why:ParameterNameDiscoverer is used by Spring MVC to find the real names of method parameters, which helps match query parameters with @RequestParam, @PathVariable, and @RequestHeader without explicitly specifying the name.
2. Which two compiler options allow Spring MVC to resolve method parameter names?
Why: Spring MVC can only match HTTP request parameters to handler method parameters if the source code is compiled with either the -g:vars option or the -parameters option of javac.
3. What is the default implementation of ParameterNameDiscoverer used by Spring?
Why: By default, Spring uses DefaultParameterNameDiscoverer, which delegates to other discoverers to find parameter names.
4. Which discoverer does DefaultParameterNameDiscoverer try first?
Why:DefaultParameterNameDiscoverer first delegates to StandardReflectionParameterNameDiscoverer, which relies on the -parameters compiler flag.
5. What does StandardReflectionParameterNameDiscoverer rely on to find parameter names?
Why:StandardReflectionParameterNameDiscoverer uses the Java reflection API and relies on the -parameters compiler flag to obtain real parameter names.
6. What does LocalVariableTableParameterNameDiscoverer rely on to find parameter names?
Why:LocalVariableTableParameterNameDiscoverer is ASM-based and relies on the -g:vars option, which includes local variable names in the class file.
7. What happens if StandardReflectionParameterNameDiscoverer returns null for a parameter name?
Why: If StandardReflectionParameterNameDiscoverer returns null, DefaultParameterNameDiscoverer attempts to use the ASM-based LocalVariableTableParameterNameDiscoverer as a fallback.
8. Why did many Spring MVC projects work without special compiler flags before Spring 6.1?
Why: Before Spring 6.1, LocalVariableTableParameterNameDiscoverer could infer parameter names from debug symbols included in bytecode by default. The default Maven build included enough debug information for Spring to resolve parameter names automatically.
9. What changed in Spring 6.1 regarding parameter name discovery?
Why: From Spring 6.1 onwards, the fallback to LocalVariableTableParameterNameDiscoverer was removed, making the -parameters flag mandatory for parameter name resolution to work correctly.
10. What error occurs when parameter name information is not available in the class file?
Why: When Spring cannot find parameter name information, it throws an IllegalArgumentException stating that the name for the argument type is not available and parameter name information was not found in the class file.
11. Can LocalVariableTableParameterNameDiscoverer be used outside of the Spring container?
Why:LocalVariableTableParameterNameDiscoverer can be used outside of the Spring container. It is a standalone class that can be instantiated and used directly in a plain Java application.
12. What does Parameter#getName() return if the code is NOT compiled with the -parameters flag?
Why: If the code is not compiled with the -parameters flag, Parameter#getName() returns a synthetic name such as arg0 instead of the real parameter name.
13. What does the Maven compiler plugin do by default regarding debug information?
Why: The Maven Java compiler plugin, by default, adds all debug information via the debug option. This includes enough information for Spring to resolve parameter names automatically in older Spring versions.
14. What is recommended for projects relying on automatic parameter name discovery to avoid future breakage?
Why: It is recommended to explicitly add the -parameter or -g option in the Maven compiler plugin configuration to ensure automatic parameter name discovery continues to work reliably.
15. With the -parameters flag enabled, which discoverer handles parameter name resolution more reliably?
Why: With the -parameters flag enabled, StandardReflectionParameterNameDiscoverer handles parameter name resolution more reliably and without bytecode parsing hacks.
16. What is the easiest fix if you see parameter name errors after upgrading to Spring 6.1?
Why: If you see errors after upgrading to Spring 6.1, the fix is almost always just enabling the -parameters compiler flag in your build configuration.
17. Which annotation is used in a controller method to automatically bind a query parameter without explicitly specifying its name?
@RequestMapping("/user")
public String test(String name) { ... }
Why:@RequestParam is used to bind a query parameter to a method parameter. If the parameter name matches the query parameter name, you do not need to specify the name explicitly, provided parameter name discovery is working.
18. Which Spring interface is responsible for finding parameter names from a method?
Why:ParameterNameDiscoverer is the Spring interface used to find parameter names from a method.
19. What information does DefaultParameterNameDiscoverer use as a fallback if reflection-based discovery fails?
Why: If reflection-based discovery fails, DefaultParameterNameDiscoverer falls back to LocalVariableTableParameterNameDiscoverer, which reads the local variable table from the bytecode.
20. Which type of application is most affected by the removal of automatic parameter name discovery in Spring 6.1?
Why: Applications that previously worked because the default Maven build included debug information (without explicitly adding the -parameters flag) are most affected, as they may now need to add the flag explicitly.