测试restful API 设计
- form表单测试restful
- ajax测试restful
5种提交类型(method)
GET(SELECT):从服务器取出资源(一项或多项)
POST(CREATE):在服务器新建一个资源。
PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源)。
PATCH(UPDATE):在服务器更新资源(客户端提供改变的属性)
DELETE(DELETE):从服务器删除资源。
表单测试restful
首先创一个实体类进行传参测试
Student.class
public class Student {
String stname;
int age;
Date birthday;
public String getStname() {
return stname;
}
public void setStname(String stname) {
this.stname = stname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"stname='" + stname + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}
再创表单进行测试
核心代码如下:
<h1>测试--ADD</h1>
<form action="student" method="post">
Name<input type="text" name="stname">
Age<input type="text" name="age">
Birthday<input type="date" name="birthday">
<input type="submit" value="ADD" >
</form>
<h1>测试--FIND</h1>
<form action="student" method="get">
<input type="submit" value="FIND" >
</form>
<h1>测试--DEL 失败</h1>
<form action="student/110" method="post">
<input type="hidden" name="_method" value="delete"/>
<input type="submit" value="DEL" >
</form>
<h1>测试--UPDATE 失败</h1>
<form action="student" method="post">
<input type="hidden" name="_method" value="put"/>
Name<input type="text" name="stname">
Age<input type="text" name="age">
Birthday<input type="date" name="birthday">
<input type="submit" value="UPDATE" >
</form>
再创建实现类RestfulController.java
public class RestfulController {
@RequestMapping(path = "/student",method = RequestMethod.POST)
public String add(Student student){
System.out.println("add...."+student);
return "success";
}
@RequestMapping(path = "/student/{sid}",method = RequestMethod.DELETE)
public String del(@PathVariable("sid") int sid){
System.out.println("del...."+sid);
return "success";
}
@RequestMapping(path = "/student",method = RequestMethod.PUT)
public String update(Student student){
System.out.println("update...."+student);
return "success";
}
@RequestMapping(path = "/student",method = RequestMethod.GET)
public String find(){
System.out.println("find....");
return "success";
}
}
测试后得知ADD和FIND方法可用
由于浏览器只支持post和get请求,所以我们得进行配置
web.xml
<!-- 将POST请求转换为DELETE或者是PUT
要用_method指定真正的请求方法-->
<filter>
<filter-name>MyHttp</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyHttp</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
表单中只要给name为_method的value设置为get或者post即可.
并且tomcat的版本需要在8.0以下才支持.
AJAX测试restful
代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ajax restful测试</title>
<script src="js/jquery.min.js"></script>
<script>
$(function () {
$("button").eq(0).click(function () {
console.log("add")
$.ajax({
url:"student",
method:"post",
data:{
"stname":"temo",
"age":18
},
success:function (date) {
alert(date)
}
})
});
$("button").eq(1).click(function () {
console.log("del")
$.ajax({
url:"student/123",
method:"delete",
success:function (date) {
alert(date)
}
})
});
$("button").eq(2).click(function () {
console.log("update")
$.ajax({
url:"student",
method:"post",
data: {
"stname":"temo",
"age":18,
"_method":"put"
},
success:function (date) {
alert(date)
}
})
});
$("button").eq(3).click(function () {
console.log("find")
$.ajax({
url:"student",
method:"get",
success:function (date) {
alert(date)
}
})
});
$("#btn").click(function () {
console.log("带身份信息")
$.ajax({
method:"GET",
url:"ajaxlogin",
headers:{'token':"123142321312"},
data:{
"stname":"temo",
"age":18,
"birthday":"2020-1-1"
}
})
});
})
</script>
</head>
<body>
<button>add</button>
<button>del</button>
<button>update</button>
<button>find</button>
</body>
总结
- 实现类中的@RequestMapping注释需加入参数:path(路径)method(传值方法如:RequstMethod.POST)
- 用ajax测试需要在实现类上方加入注释@ResponseBody表明该实现类为ajax请求
- 使用restful必须保证tomcat版本为8.0以下并且需要在web,xml配置相应的信息
- 跨域问题只需要在该方法上加上注释@CrossOrigin //支持跨域