在学习Spring Boot中,经常会用到各种各样的注解,看到别人总结得好的文章就顺手转载过来了,也让更多人看到!!!
在spring MVC中,两者的作用都是将request里的参数的值绑定到contorl里的方法参数里的,区别在于,URL写法不同。
使用@RequestParam时,URL是这样的:http://host:port/path?参数名=参数值
使用@PathVariable时,URL是这样的:http://host:port/path/参数值
例如:
@RequestMapping(value="/user",method = RequestMethod.GET)
public @ResponseBody
User printUser(@RequestParam(value = "id", required = false, defaultValue = "0")
int id) {
User user = new User();
user = userService.getUserById(id);
return user;
}
@RequestMapping(value="/user/{id:\\d+}",method = RequestMethod.GET)
public @ResponseBody
User printUser2(@PathVariable int id) {
User user = new User();
user = userService.getUserById(id);
return user;
}
转自:http://blog.csdn.net/guge907/article/details/44923983