How do I retrieve query parameters in a Spring Boot controller?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot is a powerful framework that simplifies development with Java. Often, when building web applications, you need to handle incoming HTTP requests and extract query parameters. Query parameters are key-value pairs sent in the URL following a question mark ?. In a Spring Boot application, you can easily retrieve these parameters in your controller classes using the @RequestParam annotation. This article will delve into the ways you can retrieve query parameters, offering code examples and technical explanations where necessary.
Retrieving Query Parameters in Spring Boot
The primary way to retrieve query parameters in Spring Boot is by using the @RequestParam annotation on method parameters within a controller. This annotation binds a request parameter to a method parameter within the handler method.
Using @RequestParam
When a client sends a request to a Spring Boot application with query parameters, each parameter can be captured and utilized by the application logic. Here's a simple example illustrating how to use @RequestParam in your controller:
In this example:
- The
@GetMappingannotation maps HTTP GET requests onto thegreetUsermethod. - The
@RequestParamannotation is used to bind thenamequery parameter to the Java method parametername. - The
requiredattribute is set tofalse, making it an optional parameter. If not provided, thedefaultValueof "World" is used, ensuring the application remains robust against missing parameters.
Handling Multiple Query Parameters
If you have multiple query parameters, you can add additional method parameters, each annotated with @RequestParam.
Handling Complex Parameters with Maps
For handling a dynamic set of parameters or when dealing with a large number of query parameters, using a @RequestParam of type Map<String, String> can be beneficial:
In this example, all query parameters are captured into a map and can be processed programmatically.
Default Values and Required Parameters
- Default Values: By specifying the
defaultValueattribute in@RequestParam, you can ensure that a default is used when the parameter is not provided, which is essential for optional parameters. - Required Parameters: By default,
@RequestParamassumes all parameters are required. If a required parameter is missing, Spring will throw aMissingServletRequestParameterException. To make a parameter optional, setrequired=false.
Table Summarizing Key Points
| Feature | Usage Example | Description |
Basic @RequestParam | @RequestParam String param | Binds query parameters to method parameters. |
| Optional Parameters | @RequestParam(name = "param", required = false) | Sets parameters as optional, avoiding exceptions if missing. |
| Default Value | @RequestParam(defaultValue = "default") | Provides a default value if the parameter is not provided. |
| Multiple Parameters | @RequestParam String param1, @RequestParam int param2 | Handles multiple query parameters in one method. |
| Dynamic Parameters using Map | @RequestParam Map<String, String> params | Captures all query parameters into a map. |
Conclusion
Retrieving query parameters in a Spring Boot application is both straightforward and flexible, thanks to the @RequestParam annotation. Whether you need to retrieve a single parameter or handle multiple, optional, or default values, Spring Boot provides the necessary tools to manage these requirements effectively. Understanding these methods will enable you to handle web requests with more precision and build powerful web applications with little complexity.
Related reading
- How do I retrieve query parameters in a Spring Boot controller?
- How do I reverse an int array in Java?
- How do I run a Java program from the command line on Windows?
- How do I run a spring boot executable jar in a Production environment?
- How do I save a String to a text file using Java?
- How do I set a JLabel's background color?
- How do I set environment variables from Java?
- How do I set the default locale in the JVM?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.