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

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

----------

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

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

springmvc rest实现

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

@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(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中带参数,则配合使用,如:

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

@RequestMapping(value="/blog/{blogId}/message/{msgId}",method=RequestMethod.DELETE)   


public ModelAndView delete(@PathVariable("blogId") Long blogId,@PathVariable("msgId")    

    Long msgId,HttpServletRequest request,HttpServletResponse response) {   
       
}  

相关推荐