分享

Spring MVC3.0的RESTFul方式的访问

 李副营长 2014-12-16

 

上一篇以简单介绍了一点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请求.

Java代码  收藏代码
  1. <span style="">@RequestMapping(value="/userManagerContoller/{id}",method=RequestMethod.DELETE)  
  2. public ModelAndView delete(@PathVariable Long id,HttpServletRequest request,  
  3.     HttpServletResponse response) {  
  4.     userManager.removeById(id);  
  5.     return new ModelAndView(LIST_ACTION);  
  6. }</span>  

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

Java代码  收藏代码
  1. <span style="">@RequestMapping @PathVariable如果URL中带参数,则配合使用,如:  
  2. @RequestMapping(value="/userManagerContoller/{userId}/message/{msgId}",method=RequestMethod.DELETE)  
  3. public ModelAndView delete(@PathVariable("userId") Long userId,@PathVariable("msgId")   
  4.     Long msgId,HttpServletRequest request,HttpServletResponse response) {  
  5.       
  6. }</span>  

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多