Explore the essential concepts of managing dynamic parameters in Spring Boot backend applications. This quiz covers key annotations, parameter extraction methods, and best practices for handling various types of request parameters efficiently and securely.
Which annotation is commonly used in Spring Boot to bind a query parameter from the URL to a method argument?
Explanation: @RequestParam is used to extract and bind a query parameter from the request URL to a controller method argument. @PathVariable is used for extracting template variables from the URI, not query parameters. @RequestBody binds request data to an object, and @Autowired is used for dependency injection, not parameter handling.
If you want to capture a value from the URL path like /users/5, which annotation should you use in Spring Boot?
Explanation: @PathVariable binds values from the URI path directly to method arguments, making it ideal for capturing things like IDs or names from the endpoint. @RequestParam is for query parameters, @Value is for reading configuration values, and @Param is not used for extracting path variables in controllers.
How can you provide a default value for a query parameter using @RequestParam in Spring Boot?
Explanation: The defaultValue attribute within @RequestParam allows you to specify a fallback value if the query parameter is absent in the request. Setting required=false makes the parameter optional but does not assign a default. @Default is not a valid annotation, and declaring the method argument as final does not affect the default value.
Which approach allows a Spring Boot controller to handle a query parameter that may not always be present?
Explanation: Setting required=false in the @RequestParam annotation makes the query parameter optional, allowing requests to succeed even if the parameter is missing. Using @PathVariable does not deal with query parameters, declaring a parameter as static is incorrect, and setting a value to zero does not handle parameter presence.
In Spring Boot, which type can you use to collect all query parameters in a controller method?
Explanation: A Map<String, String> can be used with @RequestParam to capture all query parameters, pairing parameter names with their values. A List<String> or Set<String> does not associate names with values, and Queue<String> is not used for parameter extraction.
What is the primary benefit of using @PathVariable in dynamic REST endpoints?
Explanation: @PathVariable enables a controller to extract dynamic segments from the path, allowing endpoints like /users/5 to handle different values. It does not validate user input, bind JSON, or auto-generate documentation, which are functions of other tools or annotations.
How can a Spring Boot method receive multiple values for the same query parameter key (e.g., ids=1&ids=2)?
Explanation: Using an array or List as the method argument type allows the framework to automatically bind repeated parameters like ids=1&ids=2 to a Java collection. @PathVariable is not for query parameters, splitting strings manually is unnecessary, and @Autowired is unrelated.
Which is a recommended way to handle multiple optional search filters in a GET endpoint in Spring Boot?
Explanation: Multiple optional filters can be implemented using @RequestParam with required=false, enabling users to supply any combination of values. @RequestBody with a map is more suited to POST requests, @PathVariable is for mandatory path elements, and ignoring missing parameters may cause ambiguity.
When should you use @RequestBody instead of @RequestParam for parameter binding in a Spring Boot controller?
Explanation: @RequestBody is used for binding the entire body of a request, such as JSON payloads or form data, to a Java object. URL template variables are handled by @PathVariable, single query parameters by @RequestParam, and dependency injection does not involve parameter binding.
What happens if you use @RequestParam for a parameter typed as int, but the provided value cannot be converted?
Explanation: A conversion error will be thrown if the value cannot be converted to the declared type, resulting in a runtime exception. The value does not silently default to zero, is not ignored, and is not coerced into a string for an int parameter.
How does Spring Boot handle URL-encoded special characters (like spaces or symbols) in query parameters?
Explanation: Spring Boot automatically decodes URL-encoded special characters before binding them to method arguments. It does not store raw encoded values, ignore characters, or replace them with underscores, ensuring the developer receives the correct data.
Can you use @RequestParam and @PathVariable in the same Spring Boot controller method, and what is a common use-case?
Explanation: You can use both annotations in a single method to extract data from both the URL path and query string, such as retrieving a user by ID and filtering by status. They are not mutually exclusive, are not limited to GET methods, and do not require POST requests.
What happens if a required @PathVariable is missing from a request to a Spring Boot endpoint?
Explanation: Spring Boot will return an error, typically a 404, if a required path variable is missing. It does not assign a random value, proceed with null, or use an automatic default, maintaining strict mapping requirements.
What is a benefit of using Map<String,String> with @RequestParam in controller methods?
Explanation: Using a Map with @RequestParam collects all query parameters into key-value pairs without explicitly naming them. It does not restrict access, disable type checking, or is required for POST requests, which are incorrect uses.
Which built-in annotation can you use for validating query parameter values, such as ensuring a number is positive?
Explanation: @Positive is a validation annotation ensuring numeric parameters are positive. @NotFound is not a validation annotation, @Validated enables validation but does not specify rules, and @Autowired is for dependency injection, not validation.
By default, what happens if a required @RequestParam is missing from the request in a Spring Boot controller?
Explanation: If a required @RequestParam is not provided and no defaultValue is set, Spring Boot returns a 400 error, signaling a bad request. An empty string is not automatically used; the parameter is not set to null, and no error is not correct behavior for required parameters.