Request Parameters
Path variables identify. Query parameters filter.
The Swiggy Filter Analogy:
When you search for Biryani on Swiggy, you don't just go to /biryani. You apply filters: "Rating must be 4.0+" and "Pure Veg only". Swiggy's URL transforms into something like: /search?food=biryani&rating=4.0&veg=true.
These values after the ? are Query Parameters. @RequestParam in Spring Boot is the tool that reads these filtering values from the URL.
- Syntax: The URL format is
endpoint?key1=value1&key2=value2. - @RequestParam String author: This extracts the
?author=Devavalue from the URL and assigns it to theauthorvariable. - Optional vs Required: By default,
@RequestParamisrequired = true. If the user doesn't provide it, Spring will throw an error. To prevent this, we use@RequestParam(required = false).
package com.devinhyderabad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestController
@RequestMapping("/api/books")
class BookController {
private List books = new ArrayList<>(List.of(
new Book(101, "Hyderabad History", "Sai"),
new Book(102, "Spring Boot Guide", "Deva"),
new Book(103, "Angular Basics", "Deva")
));
// 1. Maps GET requests to exactly /api/books (no path variables)
@GetMapping
public List getBooks(
// 2. 'required = false' means if user doesn't send ?author=..., it won't crash
@RequestParam(required = false) String author) {
if (author == null) {
return books; // Return all books if no author filter is provided
}
// 3. Filter the list based on the query parameter
return books.stream()
.filter(book -> book.getAuthor().equalsIgnoreCase(author))
.collect(Collectors.toList());
}
}
class Book {
private int id;
private String title;
private String author;
public Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}
public int getId() { return id; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
} Test this by going to http://localhost:8080/api/books?author=Deva in your browser. You will only see the books written by Deva.
Interview Question: "What is the difference between @PathVariable and @RequestParam?"
Answer: @PathVariable extracts values directly from the URI path structure (e.g., /books/101 -> 101). It is used for mandatory resource identifiers. @RequestParam extracts values from the query string after the ? (e.g., /books?author=Deva). It is typically used for filtering, sorting, or pagination, and can be made optional.
Enterprise Note: For pagination, enterprise APIs strictly use @RequestParam. Example: /api/employees?page=0&size=10&sort=lastName,asc. Spring Data JPA (which we will learn in Phase 4) actually provides a Pageable interface that automatically maps these query parameters for you!
Key Takeaways
- ✅ @RequestParam extracts values from query string after the ?
- ✅ Use required=false to make query parameters optional
- ✅ Query parameters are perfect for filtering, sorting, and pagination
- ✅ Multiple params: /books?author=Deva&rating=4.0&sort=title,asc
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login