Differences between @RequestParam and @PathVariable in Spring MVC

 @RequestParam and @PathVariable in Spring MVC

Both annotations @RequestParam and @PathVariable in Spring MVC are used for fetching the values of request parameters. These annotations have similar purpose but some differences in use. The key difference between @RequestParam and @PathVariable is that @RequestParam used for accessing the values of the query parameters where as @PathVariable used for accessing the values from the URI template.
Popular Tutorials
spring certification
@RequestParam
 It is used to get the request parameters. @RequestParam automatically binds the request parameters to the arguments of your handler method. It also provides auto type conversion for some standard type like int, long, float, string, date etc.

Look at the following request URL:

http://localhost:8080/tutorials/bookmark/?site=dineshonjava&id=200

In the above URL request, the values for site and id can be accessed as below:
@RequestMapping(value = "/tutorials/bookmark")
public String bookmark(
    @RequestParam(value="site", required=true) String site,
    @RequestParam(value="id", required=false) String id){
...
}

Read More about it.

@PathVariable
It is used to pass parameter along with the url, sometimes we need to pass parameters along with the url to get the data. Spring MVC provides support for customizing the URL in order to get data. To achieving this purpose @PathVariable annotation is used in Spring mvc framework.

Look at the following request URL:

http://localhost:8080/tutorials/bookmark/100?site=dineshonjava&id=200

In the above URL request, the values for site and id can be accessed as below:

@RequestMapping(value = "/tutorials/bookmark/{siteId}")
public String bookmark(
    @PathVariable(value="siteId") String siteId
    @RequestParam(value="site", required=true) String site,
    @RequestParam(value="id", required=false) String id){
...
}

value- It is String type attribute amd it is only one attribute of the @PathVariable annotation. It is allowed to use the multiple @PathVariable annotation in the single method. But, ensure that no more than one method has the same pattern.

Example of @PathVariable and @RequestParam
In this example we demonstrate the key difference between @PathVariable and @RequestParam annotations.

TutorialController.java

@Controller
package com.doj.webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Dinesh.Rajput
 *
 */
@Controller
public class TutorialController {

    @RequestMapping(value = "/tutorials/bookmark/{siteId}")
    public String bookMarkTutorial (
       @PathVariable(value="siteId") String siteId
       @RequestParam(value="site", required=true) String site,
       @RequestParam(value="id", required=false) String id
              Model model) {
        model.addAttribute("I have bookmarked tutorial : " + site);
        return "success";
    }
}

Application.java
@Controller
package com.doj.webapp.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
 * @author Dinesh.Rajput
 *
 */
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter{
 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 } 
}
}
Summary
This tutorial we have explained you to understand the difference between @PathVariable and @RequestParam in the Spring MVC. Basically purpose of both annotation in the same but way of fetching the request parameter value is different. @RequestParam annotation fetch the value of request parameter in the form of passing request parameter with url but @PathVariable annotation fetching value of the parameter in the form request URI template with some placeholder.

Spring MVC Related Posts
  1. Spring MVC Web Tutorial
  2. Spring MVC Interview Questions
  3. MVC Design Pattern
  4. Spring MVC DispatcherServlet
  5. Spring MVC WebApplicationContext and Root Application Context
  6. Spring MVC @Controller Annotation
  7. Spring MVC @RequestMapping Annotation
  8. Spring MVC @RequestParam Annotation
  9. Spring MVC ContextLoaderListener
  10. Spring MVC InternalResourceViewResolver
  11. Spring MVC Hello World Example
  12. Spring MVC Exception Handling Example
  13. Spring MVC with Hibernate CRUD Example
  14. Spring MVC Tiles Plugin with Example
  15. Spring MVC Interceptor with example
  16. Spring MVC with MongoDB CRUD Example
  17. Spring MVC Internationalization & Localization with Example


Labels: , ,