1、概述

  HTTP协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE,它们分别对应四种基本的操作,GET用来获取资源,POST用来新建资源,PUT用来更新资源,DELETE用来删除资源。
    
    示例:
    —— /order/1        HTTP GET:得到id=1的order
    —— /order/1     HTTP DELETE:删除id=1的order
    —— /order/1     HTTP PUT:更新id=1的order
    —— /order        HTTP POST:新增order

2、如何发送一个PUT和DELETE请求?
    常见的有post和get请求,那么怎么弄一个put和delete请求出来呢?浏览器的form表单只支持GET和POST请求,而DELETE和PUT请求并不支持。为解决这个题,Spring3.0添加了一个 HiddenHttpMethodFilter 过滤器,可以将带有_method参数的http post请求转换为put或delete请求。从而解决问题。

  如何发送PUT和DELETE请求呢?

  ①、需要在web.xml文件中配置一个HiddenHttpMethodFilter:

<!-- 配置org.springframework.web.filter.HiddenHttpMethodFilter:可以把POST请求转化为DELETE或PUT请求 -->
<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>

  ②、需要发送post请求

  ③、在发送post请求时携带一个 name="_method" 的隐藏域,其值为delete或者是put。如下:

<!-- 发送put请求 -->
<form action="rest/put/1" method="post">
<input type="hidden" name="_method" value="put" >
<input type="submit" value="3、tetRest PUT请求">
</form> <!-- 发送delete请求 -->
<form action="rest/delete/1" method="post">
<input type="hidden" name="_method" value="delete" >
<input type="submit" value="4、tetRest DELETE请求">
</form>

  ④、在接受PUT或DELETE请求方法的注解@RequestMapping中设定method为PUT或DELETE,如下:

@RequestMapping(value="/rest/put/{id}", method=RequestMethod.PUT)
public String testRestPut(@PathVariable Integer id){
System.out.println("testRestPut 方法,响应PUT请求。id = " + id); return SUCCESS;
} @RequestMapping(value="/rest/delete/{id}", method=RequestMethod.DELETE)
public String testRestDelete(@PathVariable Integer id){
System.out.println("testRestDelete 方法,响应DELETE请求。id = " + id); return SUCCESS;
}

3、REST风格的URL的更多相关文章

  1. Spring MVC 使用HiddenHttpMethodFilter配置Rest风格的URL

    /** Rest 风格的 URL. 以 CRUD 为例: 新增: /order POST 修改: /order/1 PUT update?id=1 获取:/order/1 GET get?id=1 删 ...

  2. 介绍两种风格的URL

    两种风格的“动态资源”——统一资源定位符(Uniform Resource Lactor,URL) 当前互联网上主要有两种主要风格的URL: 第一种直接在URL中知名文件(比如xxx.php,xxx. ...

  3. spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法

    spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...

  4. 如何在SpringMVC中使用REST风格的url

    如何在SpringMVC中使用REST风格的url 1.url写法: get:/restUrl/{id} post:/restUrl delete:/restUrl/{id} put:/restUrl ...

  5. Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL

    <!-- 4.使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 --> 详细使用请参考这篇博客:地址:http://blog.csdn.net/ppl ...

  6. Spring MVC REST 风格的 URL

    前言 本文主要内容为 REST 风格的 URL. REST REST(Representational State Transfer).(资源)表现层状态转化.它是一种架构风格,用 url 来访问网络 ...

  7. springMVC的rest风格的url请求

    rest是一个架构风格,用url来访问网络上的任何资源.rest的一种思想就是用http中的动作get,post,put,delete,来进行增删改查. 这里介绍的是springMVC的rest请求. ...

  8. springmvc REST风格的URL

    1:需要配置一个filter <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELET ...

  9. RESTFUL风格的URL请求及参数接收

    RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义.RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT调用移动网络资源 ...

  10. springMvc发布restFull风格的URL

    package zpark.controller; import org.springframework.stereotype.Controller; import org.springframewo ...

随机推荐

  1. How to begin Python learning?

    如何开始Python语言学习? 1. 先了解它,Wiki百科:http://zh.wikipedia.org/zh-cn/Python 2. Python, Ruby等语言来自开源社区,社区的学法是V ...

  2. 批量修改文件后缀(Python)

    近期下载了很多各种教程, 但是不幸的是后缀名都是 ".mp4", 而本人喜欢 ".rmvb" 后缀,由于有轻微洁癖, 受不了后面的 ".mp4&quo ...

  3. Zend-MVC事件

    Zend\Mvc\MvcEvent继承自Zend\EventManager\Event,在Zend\Mvc\Application::bootstrap()执行时触发.如果你的控制器实现了Zend\M ...

  4. elipse+pydev+python开发arcgis脚本程序

    环境配置参考:http://www.cnblogs.com/halfacre/archive/2012/07/22/2603848.html 添加arcpy类库.arctoolbox.arcgis-b ...

  5. Protocol Buffer使用

    Protocol Buffer使用简介 字数2630 阅读5067 评论1 喜欢12 我们项目中使用protocol buffer来进行服务器和客户端的消息交互,服务器使用C++,所以本文主要描述pr ...

  6. JS类库函数收集中....

    实现string的substring方法 方法一:用charAt取出截取部分 String.prototype.mysubstring=function(beginIndex,endIndex){ v ...

  7. Caffe安装教程(原创)

    转载请注明地址 说明:本文档参考自Caffe官网的安装说明,http://caffe.berkeleyvision.org/installation.html 如果对安装过程中,需要用到的依赖不明,请 ...

  8. 视频FMS服务器带宽成本分析

    一.现状 调查了一下,主要有两种主流方式,WebRTC或者Flash. 1. WebRTC(不支持IE浏览器,已排除):网页实时通信(英语:Web Real-Time Communication)的缩 ...

  9. semantic

    cgfx 里会有这个 float4X4 View : View; :后面这个 view 是一种 叫做user defined semantic provide the correct data to ...

  10. 01-03-03【Nhibernate (版本3.3.1.4000) 出入江湖】cascade的测试

    相关文章: http://www.cnblogs.com/amboyna/archive/2008/02/18/1072260.html注意上面是hibernate,不是Nhibernate,这解释是 ...