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.

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){
...
}
  • defaultValue– It is String type attribute and the default value to use as a fallback when the request parameter is not provided or has an empty value.
  • name– It is String type attribute and name of the request parameter to bind to.
  • required– It is Boolean type attribute whether the parameter is required.
  • value– It is String type attribute and it is alias for name attribute.

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.

Previous
Next