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.
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){ ... }
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); } } }
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.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…