Master 6 Essential Spring Boot Annotations for Reading Request Parameters
This article explains six key Spring Boot annotations—@RequestParam, @PathVariable, @MatrixVariable, @RequestBody, @RequestHeader, and @CookieValue—detailing how each retrieves different kinds of request data with clear code examples for building robust APIs.
When developing APIs with Spring Boot, reading request parameters is a fundamental operation, and Spring Boot provides several annotations to handle different API design needs.
Below are six commonly used ways to retrieve request parameters.
@RequestParam
This is the most frequently used annotation to load parameters that appear after the ? in a URL, e.g., /user?name=didispace. Example:
@GetMapping("/user")
@ResponseBody
public User findUserByName(@RequestParam("name") String name){
return userRepo.findByName(name);
}@PathVariable
Used in RESTful APIs to bind variables embedded in the URL path, such as /user/1. Example:
@GetMapping("/user/{id}")
@ResponseBody
public User findUserById(@PathVariable("id") String id){
return userRepo.findById(id);
}@MatrixVariable
Less common; some systems use matrix variables separated by ; in the URL, e.g., /books/reviews;isbn=1234;topN=5;. Example:
@GetMapping("/books/reviews")
@ResponseBody
public List<BookReview> getBookReviews(@MatrixVariable String isbn, @MatrixVariable Integer topN){
return bookReviewsLogic.getTopNReviewsByIsbn(isbn, topN);
}@RequestBody
Used to bind the body of POST/PUT requests containing complex payloads. Example:
@PostMapping("/add")
public boolean addAccounts(@RequestBody List<Account> accounts) throws SQLException {
accounts.stream().forEach(a -> {
a.setCreatedOn(Timestamp.from(Instant.now()));
a.setLastLogin(Timestamp.from(Instant.now()));
});
return notificationLogic.addAccounts(accounts);
}@RequestHeader
Retrieves values from HTTP request headers, such as an Authorization token. Example:
@GetMapping("/user")
@ResponseBody
public List<User> getUserList(@RequestHeader("Authorization") String authToken){
return userRepo.findAll();
}@CookieValue
Reads cookie data, for example a SessionId. Example:
@GetMapping("/user")
@ResponseBody
public List<User> getUserList(@CookieValue(name = "SessionId") String sessionId){
return userRepo.findAll();
}That concludes the overview of these six Spring Boot annotations for reading request parameters.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
