一、REST:
即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。
它结构清晰、符合标准、易于理解、扩展方便, 所以正得到越来越多网站的采用.
HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。
它们分别对应四种基本操作:
GET 用来获 取资源,
POST 用来新建资源,
PUT 用来更新资源,
DELETE 用来删除资源。
二、示例:
@Controller @RequestMapping("/rest") public class RestController { @RequestMapping(value="/user/{id}",method=RequestMethod.GET) public String get(@PathVariable("id") Integer id){ System.out.println("get"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.POST) public String post(@PathVariable("id") Integer id){ System.out.println("post"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.PUT) public String put(@PathVariable("id") Integer id){ System.out.println("put"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integer id){ System.out.println("delete"+id); return "/hello"; } }
前端URL 请求方式:(官网:www.fhadmin.org) Controller
/user/1 HTTP GET : 请求 method=RequestMethod.GET的方法
/user/1 HTTP DELETE: 请求 method=RequestMethod.DELETE的方法
/user/1 HTTP PUT: 请求 method=RequestMethod.PUT的方法
/userr HTTP POST: 请求 method=RequestMethod.POST的方法
三、浏览器只支持GET和POST请求,如何才能发起DELETE和PUT的请求呢
浏览器 form 表单只支持 GET 与 POST 请求,(官网:www.fhadmin.org) 而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一个过滤器----HiddenHttpMethodFilter,
可以将这些请求转换 为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。
1.将POST请求转化为put请求和delele请求的步骤
1).在web.xml文件中配置:
1
2
3
4
5
6
7
8
9
|
<!--
HiddenHttpMethodFilter过滤器可以将POST请求转化为put请求和delete请求! --> <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter- class >org.springframework.web.filter.HiddenHttpMethodFilter</filter- class > </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
2)在表单域中需要携带一个name值为_method,(官网:www.fhadmin.org) value值为put或者delete的参数,如下所示:
1
2
3
4
5
6
7
8
|
<form
action= "${pageContext.request.contextPath
}/user/1" method= "post" > <input
type= "hidden" name= "_method" value= "put" /> <input
type= "submit" value= "Submit" /> </form> <form
action= "${pageContext.request.contextPath
}/user/1" method= "post" > <input
type= "hidden" name= "_method" value= "delete" /> <input
type= "submit" value= "Submit" /> </form> |
此时点击Submit按钮,两个表单会分别以put 和delete点的方式进行提交