springmvc实现REST中的GET、POST、PUT和DELETE

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类,代码如下

 1 package me.elin.rect;
 2 import org.springframework.stereotype.Controller;
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RequestMethod;
 5 import org.springframework.web.bind.annotation.RequestParam;
 6 @Controller
 7 @RequestMapping("/rest")
 8 public class RESTMethod {
 9     private static final String SUCCESS = "success";
10     // 该方法接受POST传值,请求url为/rest/restPost
11     @RequestMapping(value = "restPost", method = RequestMethod.POST)
12     public String restPost(@RequestParam(value = "id") Integer id) {
13         System.out.println("POST ID:" + id);
14         return SUCCESS;
15     }
16     // 该方法接受GET传值,请求url为/rest/restGet
17     @RequestMapping(value = "/restGet", method = RequestMethod.GET)
18     public String restGet(@RequestParam(value = "id") Integer id) {
19         System.out.println("GET ID:" + id);
20         return SUCCESS;
21     }
22     // 该方法接受PUT传值,请求url为/rest/restPut
23     @RequestMapping(value = "/restPut", method = RequestMethod.PUT)
24     public String restPut(@RequestParam(value = "id") Integer id) {
25         System.out.println("PUT ID:" + id);
26         return SUCCESS;
27     }
28     // 该方法接受DELETE传值,请求url为/rest/restDelete
29     @RequestMapping(value="/restDelete",method=RequestMethod.DELETE)
30     public String restDelete(@RequestParam(value = "id") Integer id) {
31         System.out.println("DELETE ID:" + id);
32         return SUCCESS;
33     }
34 }

在web.xml中添加一个filter,用来过滤rest中的方法。代码如下

1 <filter>
2         <filter-name>HiddenHttpMethodFilter</filter-name>
3         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
4     </filter>
5     <filter-mapping>
6         <filter-name>HiddenHttpMethodFilter</filter-name>
7         <url-pattern>/*</url-pattern>
8     </filter-mapping>

在WebContent下创建index.jsp文件,添加如下内容

 1 <a href="rest/restGet?id=1">发送GET请求</a>
 2 <form action="rest/restPost" method="post">
 3     <input type="text" name="id" value="2"/>
 4     <input type="submit" value="发送POST请求"/>
 5 </form>
 6 <form action="rest/restPut" method="post">
 7     <input type="hidden" name="_method" value="PUT">
 8     <input type="text" name="id" value="3">
 9     <input type="submit" value="发送PUT请求">
10 </form>
11 <form action="rest/restDelete" method="post">
12     <input type="hidden" name="_method" value="DELETE">
13     <input type="text" name="id" value="4">
14     <input type="submit" value="发送DELETE请求">
15 </form>

其中get和post方法是html中自带的,但是不支持PUT和DELETE方法,所以需要通过POST方法模拟这两种方法,只需要在表单中添加一个隐藏域,名为_method,值为PUT或DELETE。
运行程序,index.jsp中一个超链接和三个表单分别表示了四种请求方法。

时间: 2024-08-07 01:32:46

springmvc实现REST中的GET、POST、PUT和DELETE的相关文章

SpringMVC+Mybatis架构中的问题记录

2014/08/16 记录 今天遇到个问题,折腾了我大约4个小时,好坑啊因为之前没遇到过 我的包是这么分的:com.工程名.模块名.service.impl     在spring 配置这个切面 execution(* com.工程名..*Impl.*(..)) 他 就是找不到com.工程名.模块名.service.impl下面的*Impl类 并且此配置就算配错了,他不报错,并且当我同时用junit测试,同时加载三个配置文件 springMVC.xml spring.xml spring-myb

SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释 2016-04-14 23:40 13030人阅读 评论(2) 收藏 举报 分类: SSM(7) 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿出来一起分享一下,希望有不足的地方大家批评指正~~~ 首先   这篇文章暂时只对框架中所要用到的配置文件进行解

解决SpringMVC拦截器中Request数据只能读取一次的问题

解决SpringMVC拦截器中Request数据只能读取一次的问题 开发项目中,经常会直接在request中取数据,如Json数据,也经常用到@RequestBody注解,也可以直接通过request.getParameter()从Request中取数据. 但是有时候我们要在请求到具体的业务之前做一些操作比如日志记录.数据校验.统一的处理等等,可以在拦截器中处理. 由于 request中getReader()和getInputStream()只能调用一次,我们在拦截器中获取Request中数据后

如何实现自己特定的内存管理,如何正确替换C++中的全局运算符new和delete

在谈下面的问题之前,请先看看写的这篇博客:new operator和operator new之间的区别 考虑下面的代码块,能运行,但是存在一些问题,我们将一一解答: #include <stdio.h> #include <iostream> using namespace std; class A { public: A() { throw exception(""); } void operator delete(void *p, size_t s) { c

Windows 已在 数据结构_顺序表.exe 中触发一个断点——new和delete注意事项

实现数据结构的顺序表的类时,输入,改,删,查都可以,但是最后析构函数时持续出错 错误提示"Windows 已在 数据结构_顺序表.exe 中触发一个断点" int *elem=new int(LIST_INIT_SIZE); if(!elem)cout<<"overflow"<<endl; if(leng>LIST_INIT_SIZE) cout<<"error"; else {    length=le

Java中使用HttpRequest调用RESTfull的DELETE方法接口提示:How to fix HTTP method DELETE doesn&#39;t support output

说明:无论是Spring框架还是Spring Boot的Feign形式的客户端,以下的解决方法都适用. 解决方法:直接升级JDK 1.8,这个问题是1.7的BUG. 参考: https://salesforce.stackexchange.com/questions/34624/http-method-delete-doesnt-support-output https://salesforce.stackexchange.com/questions/66097/how-to-fix-http-

SpringMVC: web.xml中声明DispatcherServlet时一定要添加load-on-startup标签

游历SpringMVC源码后发现,在web.xml中注册的ContextLoaderListener监听器只是初始化了一个根上下文,仅仅完成了组件扫描和与容器初始化相关的一些工作,并没有探测到具体每个URL应当map到哪个Controller, 哪个方法上.而剩一下的这些复杂工作都是由DispatcherServet来完成的,即应用服务器加载DispatcherServlet调用init()方法时才能触发这项工作.所以,如果在web.xml中配置DispatcherServlet时不设置 <lo

SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签

游历SpringMVC源代码后发现,在web.xml中注冊的ContextLoaderListener监听器不过初始化了一个根上下文,只完毕了组件扫描和与容器初始化相关的一些工作,并没有探測到详细每一个URL应当map到哪个Controller, 哪个方法上.而剩一下的这些复杂工作都是由DispatcherServet来完毕的,即应用server载入DispatcherServlet调用init()方法时才干触发这项工作.所以,假设在web.xml中配置DispatcherServlet时不设置

springmvc的3中路径风格

1.导入相应的jar包,文件放置情况 2.web.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http

集成JPA+springmvc+spring+EJB中的Java EE应用

EJB是sun的JavaEE服务器端组件模型,设计目标与核心应用是部署分布式应用程序.凭借java跨平台的优势,用EJB技术部署的分布式系统可以不限于特定的平台.EJB (Enterprise JavaBean)是J2EE(javaEE)的一部分,定义了一个用于开发基于组件的企业多重应用程序的标准.其特点包括网络服务支持和核心开发工具(SDK). 在J2EE里,Enterprise Java Beans(EJB)称为Java 企业Bean,是Java的核心代码,分别是会话Bean(Session