DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

Difference Between @RequestParam and @PathVariable Annotations in Spring

1. Introduction to @RequestParam and @PathVariable

In a Spring application, URL parameters and path variables are often required to capture dynamic values in HTTP requests. Both @RequestParam and @PathVariable help to bind these values to method parameters, but they do so in different contexts.

1.1 @RequestParam: Binding Query Parameters

@RequestParam is used to extract query parameters from the URL. Query parameters are the key-value pairs that appear after the "?" in the URL. They are often used to filter or search for specific data in the backend.

Example:

@GetMapping("/search")
public String search(@RequestParam String keyword, @RequestParam(required = false) Integer page) {
    return "Searching for: " + keyword + " on page: " + (page != null ? page : 1);
}
Enter fullscreen mode Exit fullscreen mode

URL : /search?keyword=SpringBoot&page=2

Result : Searching for: SpringBoot on page: 2

1.2 @PathVariable: Binding URI Path Variables

@PathVariable is used to extract values from the URI itself. This is typically seen in RESTful APIs where the resource identifier is part of the URL path.

Example:

@GetMapping("/products/{id}")
public String getProduct(@PathVariable("id") Long productId) {
    return "Product ID: " + productId;
}
Enter fullscreen mode Exit fullscreen mode

URL : /products/15

Result : Product ID: 15

2. Detailed Comparison Between @RequestParam and @PathVariable

Understanding when to use @RequestParam versus @PathVariable depends on the nature of the data you are handling in your application.

2.1 Key Differences

| Aspect | @RequestParam | @PathVariable |
| Purpose | Extracts query parameters from the URL | Extracts values from the URI path |
| Usage | For filtering, searching, or optional parameters | For identifying resources or specific data |
| Required Parameter | Optional (can have default values) | Required (part of the URL structure) |
| Common Use Case | Search filters, pagination, sorting | Resource identification (e.g., product IDs) |

2.2 When to Use Which?

  • Use @RequestParam when dealing with optional, often dynamic parameters that influence the query results.
  • Use @PathVariable when the parameter is integral to the resource you’re accessing, such as an ID or category name.

3. Conclusion

Understanding the difference between @RequestParam and @PathVariable is crucial for correctly designing RESTful APIs in Spring. Each serves its purpose depending on how you want to structure your URLs and what kind of data you need to pass.

By mastering these annotations, you can write more expressive and accurate Spring controllers, leading to better-structured and more maintainable code.

If you have any questions or need further clarification, feel free to leave a comment below!

Read posts more at : Difference Between @RequestParam and @PathVariable Annotations in Spring

Top comments (0)