springmvc实现REST中的GET、POST、PUT和DELETE
来源:互联网 发布:广联达软件开发待遇 编辑:程序博客网 时间:2024/04/29 15:35
spring mvc 支持REST风格的请求方法,GET、POST、PUT和DELETE四种请求方法分别代表了数据库CRUD中的select、insert、update、delete,下面演示一个简单的REST实现过程。
参照http://blog.csdn.net/u011403655/article/details/44571287创建一个spring mvc工程
创建一个包,命名为me.elin.rest,添加一个RESTMethod类,代码如下
package me.elin.rect;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/rest")public class RESTMethod { private static final String SUCCESS = "success"; // 该方法接受POST传值,请求url为/rest/restPost @RequestMapping(value = "restPost", method = RequestMethod.POST) public String restPost(@RequestParam(value = "id") Integer id) { System.out.println("POST ID:" + id); return SUCCESS; } // 该方法接受GET传值,请求url为/rest/restGet @RequestMapping(value = "/restGet", method = RequestMethod.GET) public String restGet(@RequestParam(value = "id") Integer id) { System.out.println("GET ID:" + id); return SUCCESS; } // 该方法接受PUT传值,请求url为/rest/restPut @RequestMapping(value = "/restPut", method = RequestMethod.PUT) public String restPut(@RequestParam(value = "id") Integer id) { System.out.println("PUT ID:" + id); return SUCCESS; } // 该方法接受DELETE传值,请求url为/rest/restDelete @RequestMapping(value="/restDelete",method=RequestMethod.DELETE) public String restDelete(@RequestParam(value = "id") Integer id) { System.out.println("DELETE ID:" + id); return SUCCESS; }}
- 在web.xml中添加一个filter,用来过滤rest中的方法。代码如下
<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>
- 在WebContent下创建index.jsp文件,添加如下内容
<a href="rest/restGet?id=1">发送GET请求</a><form action="rest/restPost" method="post"> <input type="text" name="id" value="2"/> <input type="submit" value="发送POST请求"/></form><form action="rest/restPut" method="post"> <input type="hidden" name="_method" value="PUT"> <input type="text" name="id" value="3"> <input type="submit" value="发送PUT请求"></form><form action="rest/restDelete" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="text" name="id" value="4"> <input type="submit" value="发送DELETE请求"></form>
其中get和post方法是html中自带的,但是不支持PUT和DELETE方法,所以需要通过POST方法模拟这两种方法,只需要在表单中添加一个隐藏域,名为_method,值为PUT或DELETE。
- 运行程序,index.jsp中一个超链接和三个表单分别表示了四种请求方法。
0 0
- springmvc实现REST中的GET、POST、PUT和DELETE
- springmvc实现REST中的GET、POST、PUT和DELETE
- REST统一接口 GET PUT DELETE POST
- SpringMVC 实现(Post增 Delete删 Put改 get查)
- SpringMVC rest风格(Post增 Delete删 Put改 get查)
- SpringMVC rest风格(Post增 Delete删 Put改 get查)
- REST HTTP中的POST PUT GET
- C#实现GET,PUT,POST,DELETE
- GET/POST/PUT/DELETE
- get/post/put/delete
- Rest模式get,put,post,delete含义与区别(转)
- Rest模式get,put,post,delete含义与区别
- Rest模式get,put,post,delete含义与区别
- Rest模式get,put,post,delete含义与区别(转)
- Rest模式get,put,post,delete含义与区别(转)
- Rest模式下get,put,post,delete的区别
- Rest模式get,put,post,delete含义与区别
- Rest模式get,put,post,delete含义与区别(转)
- 关于resize事件,不同浏览器的触发行为
- ******************** for循环 ********************
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- iOS开发UI-利用Quartz2D 实现基本绘图(画三角形、矩形、圆、圆弧)
- Android Canvas绘图详解
- springmvc实现REST中的GET、POST、PUT和DELETE
- 数据结构--排序之插入排序
- Android学习笔记——AVD
- lua语言的简单语句的总结
- 如何在Linux(Ubuntu 14.04 LTS)上安装搜狗拼音输入法
- ps aux指令詳解---进程
- HI3518E网络摄像机应用于智能车门遥控(RF发射,315接收,手机APP)
- LaTex初学者模板
- swft入门学习番外篇-创建项目