In spring MVC, the function of both is to bind the value of the parameter in the request to the method parameter in the contorl. The difference is that the URL is written differently.
When using @RequestParam, the URL is like this: http://host:port/path?Parameter name = parameter value
When using @PathVariable, the URL is like this: http://host:port/path/Parameter value
For example:
@RequestMapping(value="/user",method = RequestMethod.GET)
##public @ResponseBody
"id", required = false, defaultValue = "0")
# User user =
new User();## user = userService .getUserById(id);
#@RequestMapping(value=
"/user/{id:\\d+}",method = RequestMethod.GET)
## User printUser2(
@PathVariable## User user = new User();
user = userService.getUserById(id);
The above is the detailed content of Compare and contrast the usage differences between @RequestParam and @PathVariable. For more information, please follow other related articles on the PHP Chinese website!