Differences between @RequestParam and @PathVariable in Spring

While working with Spring Framework you might have used @RequestParam and @PathVariable annotations along with @RequestMapping.
Spring Framework provides many annotations and each one is having its unique behavior and advantage.
If you get insights of these annotations then you can play very well with Spring Framework.

Now lets first go with @PathVariable. Assume that we have HTTP request with URL like - http://localhost:8080/api/v1/employee/123
Now for this lets see how we can use @PathVariable to get the value of employee id from URL.

 @RequestMapping(value = "/employee/{employeeId}", method = RequestMethod.GET)
 public String getEmployee ( @PathVariable ("employeeId") long employeeId) {
  // code to get employee by employee id
 }

Now here with @PathVariable we are passing employeId. That same in our URL after /employee so if both are same then we do not need to pass employeeId inside @PathVariable annotation. We can do like below code.


 @RequestMapping(value = "/employee/{employeeId}", method = RequestMethod.GET)
 public String getEmployee ( @PathVariable long employeeId) {
  // code to get employee by employee id
 }

Now lets check @RequestParam. From annotation itself we say that here value comes in URL as query parameter. Assume that we have HTTP request with URL like - http://localhost:8080/api/v1/employee?employeeId=123


 @RequestMapping(value = "/employee", method = RequestMethod.GET)
 public String getEmployee ( @RequestParam ( value = "employeeId", required = false ) long employeeId) {
  // code to get employee by employee id
 }

required = false indicates that this query param is NOT mandatory. That means its optional. Now here query param in URL employeeId is same as our variable that we defined with @RequestParam. So if variable name is same as our query param coming in URL then we can do like below.


 @RequestMapping(value = "/employee", method = RequestMethod.GET)
 public String getEmployee ( @RequestParam long employeeId) {
  // code to get employee by employee id
 }

If you want to lean Spring Framework then just check out Spring Framework - Social SignIn, PayPal, JMS, Velocity

So we got idea of both the annotations. Now let's see differences between these two.
1) @RequestParam is used to get query parameters while @PathVariable is used to get data from URI.
2) @RequestParam is mostly useful for traditional web apps where data comes in query parameters. For ex. web form submission. While @PathVariable is useful for REST APIs.

Checkout Below Useful Courses :-
1) Learn RabbitMQ Messaging and its practical implementation with Java, Spring Boot and Spring MVC

2) Learn Spring Social SignIn with Twitter, LinkedIn. PayPal Payment gateway Integration in Spring MVC application, Java Messaging Service (JMS) using Spring Boot and Spring MVC, Apache Velocity to send Email

3) Java Messaging Service(JMS) With Spring MVC, Spring Boot, Apache ActiveMQ


4) Java Interview Preparation || 100+ Quality Questions Covered



0 Comments