在Restful风格中,现有规定如下:
- GET(SELECT):从服务器查询,可以在服务器通过请求的参数区分查询的方式。
- POST(CREATE):在服务器新建一个资源,调用insert操作。
- PUT(UPDATE):在服务器更新资源,调用update操作。
- DELETE(DELETE):从服务器删除资源,调用delete语句
如果当前url是 http://localhost:8080/User
那么用户只要请求这样同一个URL就可以实现不同的增删改查操作,例如
- http://localhost:8080/User?_method=get&id=1001 这样就可以通过get请求获取到数据库 user表里面 id=1001 的用户信息
- http://localhost:8080/User?_method=post&id=1001&name=zhangsan 这样可以向数据库user 表里面插入一条记录
- http://localhost:8080/User?_method=put&id=1001&name=lisi 这样可以将 user表里面 id=1001 的用户名改为lisi
- http://localhost:8080/User?_method=delete&id=1001 这样用于将数据库user 表里面的id=1001 的信息删除
在springMVC中实现restful风格开发
这里,我通过访问http://127.0.0.1:8080/ssmvc/restful接口的method不同来进入不同的controller方法,并打印返回数据。
- @RequestMapping(value = "/restful",method = RequestMethod.GET)
- public void list(HttpServletRequestrequest,HttpServletResponse response,TestVo vo) throws IOException {
- System.out.println("list被访问,参数:" + vo.toString());
- Map<String,Object> map= newHashMap<String, Object>();
- map.put("params",vo);
- map.put("method",RequestMethod.GET);
- response.getWriter().write(JSON.toJSONString(map));
- }
- /**
- * restful风格insert接口
- * @param request
- * @param response
- * @param vo
- * @throws IOException
- */
- @RequestMapping(value = "/restful",method = RequestMethod.POST)
- public voidupdate(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {
- System.out.println("update被访问,参数:" + vo.toString());
- Map<String,Object> map= newHashMap<String, Object>();
- map.put("params",vo);
- map.put("method",RequestMethod.POST);
- response.getWriter().write(JSON.toJSONString(map));
- }
- /**
- * restful风格update接口
- * @param request
- * @param response
- * @param vo
- * @throws IOException
- */
- @RequestMapping(value = "/restful",method = RequestMethod.PUT)
- public void add(HttpServletRequest request, HttpServletResponse response, TestVo vo) throws IOException {
- System.out.println("add被访问,参数:" + vo.toString());
- Map<String,Object> map= newHashMap<String, Object>();
- map.put("params",vo);
- map.put("method",RequestMethod.PUT);
- response.getWriter().write(JSON.toJSONString(map));
- }
- /**
- * restful风格delete接口
- * @param request
- * @param response
- * @param vo
- * @throws IOException
- */
- @RequestMapping(value = "/restful/{id}",method = RequestMethod.DELETE)
- public void del(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") String id) throws IOException {
- System.out.println("delete被访问,参数:" + ", id:"+ id);
- Map<String,Object> map= newHashMap<String, Object>();
- map.put("params",id);
- map.put("method",RequestMethod.DELETE);
- response.getWriter().write(JSON.toJSONString(map));
- }
这里要注意一下
1.html表单form中,method没有put、delete。
2.springMVC并不能直接接收到通过put、delete方式传过来的参数。
我这里的解决方式是
1.添加过滤器hiddenHttpMethodFilter,作用是将put和delete的参数获取并重新放入request中,controller便可以直接拿到这些参数。
我们需要在web.xml中配置一个过滤器
<!-- 配置过滤器 将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>
在表单上增加一个隐藏表单域,将HiddenHttpMethodFilter里的_method属性改为put或者delete后提交
因为只有form表单才具有post方法,而这个过滤器也只能将post方法转化,get则不行。
比如你要提交一个删除的请求
其Controller里的方法对应的
@RequestMapping
(value=
"deleteById/{id}"
,method =
RequestMethod.DELETE
)
//RESTFUL风格
<a
class
=
"del"
href=
"deleteById/100"
>DELETE</a>
//将ID为100的删除
<form action=
""
method=
"post"
id=
"delForm"
>
<input type=
"hidden"
name=
"_method"
value=
"
DELETE
"
>
</form>
当然这种提交是要写js的
$(function(){
$(
'.del'
).click(function(){
$(
'#delForm'
).attr(
'action'
,
this
.href).submit();
})
})
这段js脚本的意思就是
在点击<a>标签删除的时候将a标签的href赋值给id为delForm的表单的action然后让这个form
表单提交.这就完成了将POST请求转换成DELETE请求,那么PUT请求也可以同样这样做.