spring MVC之Spring MVC3.0的RESTFul方式的访问

spring MVC之Spring MVC3.0的RESTFul方式的访问

----------

 

下面的例子都是在基于Spring MVC 3.0基于注解的配置上进行的. springmvc3.0中增加RESTful URL功能,可以通过下面的方式访问,如:

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

 

 

springmvc rest实现

springmvc的resturl是通过@RequestMapping@PathVariable annotation提供的,通过如@RequestMapping(value="/blog/{id}",method=RequestMethod.DELETE)即可处理/blog/1 的delete请求.

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

 

 

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

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

你可能感兴趣的:(spring,mvc)