`

Spring MVC3.0的RESTFul方式的访问

 
阅读更多

 

上一篇以简单介绍了一点RESTFUL URL

 

Spring MVC 3.0中一个重大的变化是增加RESTFUL URL功能,可以通过下面的方式访问,如:

/userManagerContoller/1     HTTP GET => 得到id = 1的user
/userManagerContoller/1     HTTP DELETE => 删除 id = 1的user
/userManagerContoller/1     HTTP PUT => 更新id = 1的user
/userManagerContoller/1     HTTP POST => 新增user

 

 

模板中的参数可以定义多个

@RequestMapping("/welcome/{param}/{sex}")



 

 

springmvc restful实现

 

springmvc的resturl是通过@RequestMapping@PathVariable annotation提供的,通过如@RequestMapping(value="/userManagerContoller/{id}",method=RequestMethod.DELETE)

即可处理/userManagerContoller/1 的delete请求.

@RequestMapping(value="/userManagerContoller/{id}",method=RequestMethod.DELETE)
public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,
	HttpServletResponse response) {
	userManager.removeById(id);
	return new ModelAndView(LIST_ACTION);
}

 @RequestMapping @PathVariable如果URL中带参数,则配合使用,如:

@RequestMapping @PathVariable如果URL中带参数,则配合使用,如:
@RequestMapping(value="/userManagerContoller/{userId}/message/{msgId}",method=RequestMethod.DELETE)
public ModelAndView delete(@PathVariable("userId") Long userId,@PathVariable("msgId") 
	Long msgId,HttpServletRequest request,HttpServletResponse response) {
	
}

 

 

  • 大小: 14.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics