@RequestParam vs @PathVariable in Spring Boot

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Hey everyone — welcome back.

In this video, we’re going to break down the difference between two commonly used Spring Boot annotations: @RequestParam and @PathVariable.

You might have seen both used to extract data from HTTP requests. But how do they differ? When should you use one over the other?

We're going to walk through this side-by-side comparison — and by the end, you'll be able to use both with confidence in any Spring Boot REST API.

Let’s get started.

@RequestParam vs @PathVariable in Spring Boot



1. What They Do

Let’s begin with what each one actually does.

@RequestParam is used to extract data from query parameters — that’s the part of the URL that comes after the question mark. For example, if the URL is /search?keyword=laptop, keyword is a request parameter.

On the other hand, @PathVariable pulls data directly from the URI path itself. That means the value is embedded within the URL structure, like /products/42, where 42 is a path variable.

So to summarize, @RequestParam reads values from the query string, and @PathVariable reads values embedded in the actual path.

Example:

@RestController
@RequestMapping("/api/items")
public class ItemController {

    // Using @RequestParam to extract query parameter
    @GetMapping
    public String getItemByName(@RequestParam(name = "name") String name) {
        return "Item requested: " + name;
    }

    // Using @PathVariable to extract from URI path
    @GetMapping("/{name}")
    public String getItemByPath(@PathVariable("name") String name) {
        return "Item requested: " + name;
    }
}

2. Where in the URL

Now let’s talk about where they live in the URL.

@RequestParam values appear after the question mark in the URL. You might see something like /users?country=India&age=25. Those key-value pairs are request parameters.

But with @PathVariable, the values are part of the route itself. You might see something like /users/25, where 25 is directly in the URL path — not as a named parameter.

This difference affects how URLs are structured and how readable or shareable they are.


3. URL Example

Let’s take a closer look with an example.

Say we have a use case where someone wants to look up a specific user.

If we use @PathVariable, the endpoint might look like /user/101, where 101 is the ID of the user. That’s clean and direct — ideal for accessing a specific resource.

If we use @RequestParam, the same request could be written as /user?id=101. This is more flexible when passing multiple optional values, but it’s a little more verbose.

So both work — but they’re designed for slightly different purposes.


4. Use Case

Let’s talk about when to use each.

Use @PathVariable when the value is required and directly identifies a resource. It’s great for things like user IDs, order IDs, or anything where the path clearly points to one item.

Use @RequestParam when the value is optional, or when you have multiple filters, sorts, or search terms. It’s commonly used in search APIs, filters, pagination — places where flexibility matters more than structure.

Think of it like this — path variables are for what you want. Request params are for how you want it.


5. Optional vs Required

Here’s another big difference — flexibility.

@RequestParam values can be optional. You can set defaults, or skip them entirely. This makes them perfect for advanced filters like "show me products under $100, in stock, sorted by rating."

But @PathVariable values are almost always required. If you define an endpoint like /user/{id}, you can’t skip the ID — it's expected to be there.

So, @RequestParam gives you flexibility. @PathVariable gives you precision.


6. Semantics and REST

In REST API design, the two also have different semantic meanings.

@PathVariable represents a resource — like a specific user, order, or product. The URL path is saying: "Get me this resource."

@RequestParam adds detail about how you want that resource — maybe sorted, filtered, or paged. It’s about customization, not identification.

So if your API is meant to be RESTful and intuitive, placing the ID in the path — using @PathVariable — is the more semantically correct approach.


Wrap Up

Let’s wrap it all up.

@PathVariable is for values that are part of the URL path. It’s perfect for required, meaningful values that identify a resource.

@RequestParam is for values that come from the query string. It’s ideal for optional filters, settings, and extra parameters.

They both let your Spring Boot APIs receive input from the client — but the way they do it, and the situations where they shine, are quite different.

Understanding this will help you design cleaner, more maintainable REST endpoints that make sense both to you and your users.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare