Optional @RequestParam with defaultValue in Spring MVC
Fast recall
Quick revision
Core answer
Practical API design knowledge — understanding how to build flexible endpoints that don't force clients to provide every parameter
Not setting defaults for pagination parameters, forcing clients to always specify page and size instead of having sensible defaults like page=0 and size=20
Show a complete pagination example with defaultValue for both page and size, demonstrating production-ready API design
Interview-ready guide
Interview answer
A list endpoint can use @RequestParam(defaultValue = "0") int page and @RequestParam(defaultValue = "20") int size. A truly optional search term can use Optional
For example, @RequestParam(defaultValue = "20") int size gives /orders a size of 20 while /orders?size=50 supplies 50. I still validate an upper bound so a client cannot request an unbounded result.
Optional @RequestParam with defaultValue in Spring MVC
Direct Answer
Set required=false when absence should be represented as null for a nullable/reference type, use Optional<T> when explicit absence helps, or provide defaultValue when the endpoint has a real fallback. A default value implicitly makes @RequestParam non-required. Prefer a default for primitive parameters because primitives cannot represent null.
Code Example
@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductRepository productRepository;
public ProductController(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@GetMapping
public List<Product> searchProducts(
@RequestParam(required = false) String category,
@RequestParam(required = false) String name,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size,
@RequestParam(defaultValue = "name") String sortBy) {
// All these work:
// GET /api/products -> defaults: page=0, size=20, sortBy=name, category=null, name=null
// GET /api/products?category=electronics
// GET /api/products?page=2&size=10&sortBy=price
return productRepository.findProducts(category, name, page, size, sortBy);
}
}
Common Mistakes & Pitfalls
- No defaults for pagination: Freshers write
@RequestParam int page, @RequestParam int sizewithoutdefaultValue, forcing clients to always specify pagination. This makes simple requests likeGET /usersfail with 400. Always provide defaults for page (usually0) and size (usually10or20). - Using empty string as default: Writing
@RequestParam(defaultValue="") String namemeansnameis nevernull, it's an empty string when omitted. This can break repository queries that checkif (name != null). Userequired=falseinstead and handlenullexplicitly. - Primitive types with required=false: Writing
@RequestParam(required=false) int pagedoesn't work — primitives can't benull. Spring will throw an error if the parameter is missing. UseInteger(boxed type) instead:@RequestParam(required=false) Integer page, or usedefaultValuewith the primitive type.
Check your understanding
Answer these without looking back at the article.
- Why should you use Integer instead of int when @RequestParam is required=false?
- What is the difference between required=false and providing a defaultValue?
- How do you validate a @RequestParam value (e.g., size must be <= 100)?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.