How do I retrieve query parameters in a Spring Boot controller?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In a Spring Boot application, retrieving query parameters in a controller is a common task that allows you to capture parameters from the URL and use them within your application logic. This process facilitates the functionality of web applications by tailoring responses based on client queries. Here we explore various methods to fetch these parameters effectively using Spring Boot. We'll cover methods to retrieve query parameters, including explanations and examples, and introduce additional concepts to solidify your understanding.
Query Parameters in Spring Boot
Query parameters are appended to the URL and are used to pass data to the server. Consider a URL of the form:
Here, category and sort are query parameters.
Retrieving Query Parameters
In Spring Boot, you can retrieve query parameters using the @RequestParam annotation in a controller method. This approach provides a straightforward way to capture parameters.
Basic Usage of @RequestParam
To retrieve a query parameter, annotate a method argument with @RequestParam:
In this example, category and sort are captured from the URL as query parameters. The method will return a simple response containing these values.
Handling Default Values
Sometimes, query parameters may be optional. You can specify default values using the defaultValue attribute:
Here, if the sort parameter is absent, it defaults to "asc".
Specifying Parameters as Optional
Spring Boot also allows parameters to be optional by using the required attribute:
By setting required = false, the method can handle URLs missing the sort parameter.
Retrieving Multiple Parameters
For more complex scenarios, you may need to work with numerous query parameters or map them to an object. This can be efficiently managed by using a POJO to represent the parameters.
Using a POJO
First, define a class representing the parameters:
Then, use this class in the controller:
Table of Key Points
| Feature | Description | Example Usage |
Basic @RequestParam Usage | Captures mandatory query parameters | @RequestParam String category |
| Default Values | Sets default when parameter is absent | @RequestParam(defaultValue = "asc") |
| Optional Parameters | Specifies that a parameter is not required | @RequestParam(required = false) |
| Mapping to a POJO | Uses an object to manage multiple params | ProductQuery productQuery |
Additional Considerations
- Type Conversion: Spring Boot can automatically convert query parameters to various data types such as
int,double, or even custom types, provided a suitable converter is registered. - Validation: Combine
@RequestParamwith validation annotations like@NotNullor@Sizeto ensure parameter integrity. - Exception Handling: Implement global exception handlers to provide meaningful error responses when query parameters fail validation or conversion.
Conclusion
Retrieving query parameters in a Spring Boot controller is both a basic and crucial part of developing web applications. By leveraging @RequestParam, you can easily capture and use data sent by clients, making your APIs more dynamic and flexible. It's essential to understand the different techniques for handling query parameters, including their default values and optional status, to accommodate various use cases. Mapping query parameters to a POJO can also simplify management for complex requests.
Related reading
- How do I retrieve the public IP for a fargate task using the CLI?
- How do I set a default User Agent on an HttpClient?
- How do I set the proxy to be used by the JVM
- How do I to forward example.com to www.example.com at godaddy for s3 hosted site?
- 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 use basic HTTP authentication with the Python Requests library?
- How do I use minikube's DNS?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.