Six Common Ways to Read Request Parameters in Spring Boot

This article explains six typical techniques—@RequestParam, @PathVariable, @MatrixVariable, @RequestBody, @RequestHeader, and @CookieValue—for extracting request data in Spring Boot APIs, providing usage scenarios and concrete code examples for each method.

Architect
Architect
Architect
Six Common Ways to Read Request Parameters in Spring Boot

When developing APIs with Spring Boot, reading request parameters is a fundamental operation, and Spring Boot offers several mechanisms to satisfy different API design requirements.

@RequestParam

This is the most commonly used annotation for loading parameters that appear after the ? in a URL. For example, the request /user?name=didispace can be handled as follows:

@GetMapping("/user")
@ResponseBody()
public User findUserByName(@RequestParam("name") String name) {
    return userRepo.findByName(name);
}

@PathVariable

Commonly used in RESTful APIs to load parameters embedded in the URL path. For a request like /user/1, you can retrieve the id parameter with:

@GetMapping("/user/{id}")
@ResponseBody()
public User findUserById(@PathVariable("id") String id) {
    return userRepo.findById(id);
}

@MatrixVariable

Less frequently used, but some systems support matrix variables separated by semicolons. For a request such as /books/reviews;isbn=1234;topN=5;, the parameters can be accessed with:

@GetMapping("/books/reviews")
@ResponseBody()
public List<BookReview> getBookReviews(@MatrixVariable String isbn, @MatrixVariable Integer topN) {
    return bookReviewsLogic.getTopNReviewsByIsbn(isbn, topN);
}

@RequestBody

Used to bind the request body of POST/PUT requests, typically for complex payloads. Example of adding multiple accounts:

@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

Loads data from HTTP request headers, often used for infrastructure concerns such as passing a TraceID. Example of reading an Authorization header:

@GetMapping("/user")
@ResponseBody()
public List<User> getUserList(@RequestHeader("Authorization") String authToken) {
    return userRepo.findAll();
}

@CookieValue

Retrieves values from cookies, useful for stateful interactions. Example of reading a SessionId cookie:

@GetMapping("/user")
@ResponseBody()
public List<User> getUserList(@CookieValue(name = "SessionId") String sessionId) {
    return userRepo.findAll();
}

The article concludes after presenting these six annotation-based methods for handling request parameters in Spring Boot.

JavaBackend DevelopmentSpring BootAPIAnnotationsRequest Parameters
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.