@RequestParam @PathVariable 参数绑定注解
1、使用 http://localhost:8088/user/select?id=1 路径访问时,后端获取id值得方法:
1 @RequestMapping(value="/select", method= RequestMethod.GET) 2 public JsonResult selectUser(@RequestParam(value="id",required = false)Integer id){ 3 try { 4 User user=userService.selectByPrimaryKey(Integer.valueOf(id)); 5 return new JsonResult("200", "查询成功",user); 6 }catch (Exception e){ 7 return new JsonResult("500",e.getMessage()); 8 } 9 }
使用@RequestParam 注解获取id参数的值,
该注解有两个属性: value、required; value用来指定要传入值的id名称,要跟URL上面的一样;required用来指示参数是否必须绑定;
2、使用http://localhost:8088/user/select/1 路径访问时,后端获取 1 的方法:
1 @RequestMapping(value="select/{id}", method= RequestMethod.GET) 2 public JsonResult selectUser(@PathVariable("id") Integer id){ 3 4 try { 5 User user=userService.selectByPrimaryKey(Integer.valueOf(id)); 6 return new JsonResult("200", "查询成功",user); 7 }catch (Exception e){ 8 9 return new JsonResult("500",e.getMessage()); 10 } 11 }
使用@PathVariable 注解获取,当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。
PathVariable 汉语意思是:路径变量,也就是要获取一个url 地址中的一部分值, RequestMapping 上说明了@RequestMapping(value="/select/{id}"),就是想获取URL地址 /select/ 的后面的那个 {id}的。
参考网址:http://blog.csdn.net/walkerjong/article/details/7946109
http://blog.csdn.net/chuck_kui/article/details/55506723