Java Spring REST API Handling Many Optional `Parameters`
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring makes it easy to accept optional query parameters, but controller methods become hard to maintain when every filter is declared as a separate argument. Once an endpoint grows beyond a few optional fields, the better pattern is to group those filters into a request object and keep parsing, validation, and query construction separate.
Start Simple With @RequestParam
For a small endpoint, ordinary optional request parameters are fine. Mark them as not required and keep the method readable.
This works well for a handful of filters. It breaks down when the method also needs paging, sorting, validation, and feature-specific options.
Use a Parameter Object for Real APIs
Once the endpoint has many optional parameters, bind them into one object. That keeps the controller signature stable and makes validation easier.
Now the controller just receives input. The service can decide how each optional field affects the query.
Build Queries From Present Values Only
The service layer should treat optional parameters as filters that may or may not exist. Here is a simple example using conditional logic:
This keeps parsing concerns in the controller and business rules in the service. If you later move to JPA Specifications, Querydsl, or a search backend, the controller does not have to change.
Default Values and Validation
Optional does not mean unvalidated. You can still enforce ranges and sensible defaults.
Use defaults only where they match business meaning. A default page size is reasonable. A default category or default date range can silently surprise API consumers if it changes what "no filter" should mean.
Common Pitfalls
- Putting ten or fifteen
@RequestParamvalues directly in one controller method and creating an unreadable signature. - Using
Optionalfields in request DTOs. Spring can bind them, but plain nullable fields are often simpler. - Applying defaults that change business meaning instead of only filling in transport-level conveniences such as page size.
- Mixing validation, parsing, and query logic in the controller.
- Letting contradictory filters through, such as a minimum price greater than a maximum price.
Summary
- Use plain optional
@RequestParamvalues only for small endpoints. - Move larger filter sets into a parameter object bound with
@ModelAttribute. - Keep controller code thin and build query logic in the service layer.
- Validate optional values just as carefully as required ones.
- Choose defaults only when they represent stable, predictable API behavior.
Related reading
- Java Stream API - Best way to transform a list map or forEach?
- java.lang.NoClassDefFoundError org/apache/http/conn/SchemePortResolver with AmazonHttpClient
- java.lang.NoClassDefFoundErrorfailed resolution of Lorg/apache/http/ProtocolVersion
- java.net.ConnectException Connection refused
- Java Spring Security - User.withDefaultPasswordEncoder is deprecated?
- Java Stanford NLP Part of Speech labels?
- java.net.SocketException Connection reset
- java.net.SocketException socket failed EPERM (Operation not permitted)

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.