Restful 解释说明
Restful 风格解释
1、特征
1、每一个URI代表1种资源;
2、客户端使用GET、POST、PUT、DELETE4个表示操作方式的动词对服务端资源进行操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源;
3、交互数据建议 JSON
| 请求方式 | 标识 | 意图 |
|---|---|---|
| GET【查】 | http://localhost:8989/xxx/users | 查询所有用户 |
| POST【增】 | http://localhost:8989/xxx/users | 在所有用户中增加一个 |
| PUT【改】 | http://localhost:8989/xxx/users | 在所有用户中修改一个 |
| DELETE【删】 | http://localhost:8989/xxx/users/1 | 删除用户1 |
| GET | http://localhost:8989/xxx/users/1 | 查询用户1 |
| GET | http://localhost:8989/xxx/users/1/orders | 查询用户1的所有订单 |
| POST | http://localhost:8989/xxx/users/1/orders | 在用户1的所有订单中增加一个 |
2、案例
- 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery-3.4.1.min.js"></script>
</head>
<body>
<button onclick="add();">新增</button>
<button onclick="modify();">修改</button>
<button onclick="query();">查询</button>
<button onclick="del();">删除</button>
</body>
<script>
function add() {
var user = {username : "小强", password : "xiaoqiang", birthday : new Date(), gender : 1};
$.ajax({
url : "/user/option",
type : "post",
data : JSON.stringify(user),
contentType : "application/json",
success : function (result) {
alert(result);
}
});
}
function modify() {
var user = {username : "小凤", password : "xiaofeng", birthday : new Date(), gender : 0};
$.ajax({
url : "/user/option",
type : "put",
data : JSON.stringify(user),
contentType : "application/json",
success : function (result) {
alert(result);
}
});
}
function query() {
$.ajax({
url : "/user/option",
type : "get",
dataType : "json",
success : function (result) {
alert(result);
for (var i = 0; i < result.length; i++) {
alert(result[i].username + " : " + result[i].birthday);
}
}
});
}
function del() {
$.ajax({
url : "/user/option/1",
type : "delete",
success : function (result) {
alert(result);
}
});
}
</script>
</html>
- Controller
package com.qf.java2007.web.controller;
import com.qf.java2007.pojo.User;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 模块化开发
* 窄化请求映射
* 类上也标记了@RequestMapping注解
* 请求url : 类上@RequestMapping中value和方法上的@RequestMapping中value拼接在一起,产生一个完整的请求url
*
* @RequestMapping("/user")
* 可以省略 "/".
* 方法上@RequestMapping中的value可以省略后缀
*
* 强烈建议,写上完整的请求URL /user/save.do
* @author ghy
* @version 1.0
*/
@RestController
@RequestMapping("/user")
public class UserController {
/**
* /user/save
* @return
*/
@RequestMapping(value = "/option", method = RequestMethod.POST)
public String save(@RequestBody User user){
System.out.println("post:" + user);
return "success";
}
@RequestMapping(value = "/option", method = RequestMethod.PUT)
public String update(@RequestBody User user){
System.out.println("put:" + user);
return "success";
}
@RequestMapping(value = "/option", method = RequestMethod.GET)
public List<User> list(){
List<User> users = new ArrayList<>();
users.add(new User("张三", "zhangsan", new Date(), 1));
users.add(new User("张四", "zhangsi", new Date(), 0));
users.add(new User("张五", "zhangwu", new Date(), 1));
return users;
}
@RequestMapping(value = "/option/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
System.out.println("delete:" + id);
return "success";
}
}
@CookieValue
- handler
@RequestMapping("/test1")
public String test1(@CookieValue(name = "JSESSIONID", required = false) String sessionId){
System.out.println("Demo1Controller test1");
System.out.println("sessionId = " + sessionId);
return "success";
}

浙公网安备 33010602011771号