RESTful介绍
REST: 即 Representational State Transfer , ( 资源 ) 表现层状态转化 , 是目前最流行的一种互联网软件架构。它结构清晰、符合标注、易于理解、方便扩展,所以越来越多的网站采用!具体说,就是 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
Spring中实现RESTful风格
HiddenHttpMethodFilter: 浏览器 form 表单只支持 GET 和 POST, 不支持 DELETE 和 PUT 请求 , Spring添加了一个过滤器,可以将这些请求转换为标准的http方法,支持GET,POST,DELETE,PUT请求!
具体实现
web.xml添加HiddenHttpMethodFilter配置
<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>
@RestController
public class RestTestController {
@RequestMapping(value = "/user",method = RequestMethod.GET) // @GetMapping("/user")
public String getAll(){
System.out.println("查所有进来了");
return "ok";
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)// @GetMapping("/user")
public String getOne(@PathVariable("id") Integer id){
System.out.println("查询id是" + id);
return "ok";
}
@RequestMapping(value = "/user",method = RequestMethod.POST)// @PostMapping("/user")
public String add(){
System.out.println("正在添加");
return "ok";
}
//协议://ip:端口/api/version/user/{id}
@RequestMapping(value ="/user/{id}",method = RequestMethod.DELETE)// @DeleteMapping("/user/{id}")
public String delete(@PathVariable("id") Integer id){
System.out.println("正在删除id为" + id);
return "ok";
}
@RequestMapping(value = "/user",method = RequestMethod.PUT)// @PutMapping("/user")
public String update(){
System.out.println("正在更新");
return "ok";
}
}
写一个html
把表单的post改为get
如果要写delete put 则要通过上述过滤器,form表单写成下面的形式
写put delete 把value改成delete或者put action地址也记得改
如果用Ajax get post delete put 都可以用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
function testGet(){
$.ajax({
url:"/user",
type:"get",
success:function(res){
console.log(res);
}
})
}
function testGetOne(){
$.ajax({
url:"/user/1",
type:"get",
success:function(res){
console.log(res);
}
})
}
function testPost(){
$.ajax({
url:"/user",
type:"post",
success:function(res){
console.log(res);
}
})
}
function testPut(){
$.ajax({
url:"/user",
type:"put",
success:function(res){
console.log(res);
}
})
}
function testDelete(){
$.ajax({
url:"/user/1",
type:"delete",
success:function(res){
console.log(res);
}
})
}
</script>
</head>
<body>
<!-- <form action="/user/1" method="post">-->
<!-- <input type="hidden" name="_method" value="delete">-->
<!-- <input type="submit" value="提交">-->
<!-- </form>-->
<button onclick="testGet()">查所有</button>
<button onclick="testGetOne()">查一个</button>
<button onclick="testPost()">post</button>
<button onclick="testPut()">put</button>
<button onclick="testDelete()">delete</button>
</body>
</html>