目录
一、是什么?
rest全称Representational State Transfer , 中文: 表现层状态转移
是一种接口风格和设计的理念,不是标准
二、有什么用?
我们常见以前写的url地址是这样格式的:http://localhost:8080/myboot?id=1
使用了rest的url地址是这样的http://localhost:8080/myboot/1
看起来更加简便了
三、六个Rest里面使用的注解
@PathVariable : 从url中获取数据
@GetMapping: 支持的get请求方式, 等同于 @RequestMapping( method=RequestMethod.GET) 用于查找数据
@PostMapping: 支持post请求方式 ,等同于 @RequestMapping( method=RequestMethod.POST) 用于增加数据
@PutMapping: 支持put请求方式, 等同于 @RequestMapping( method=RequestMethod.PUT) 用于修改数据
@DeleteMapping: 支持delete请求方式, 等同于 @RequestMapping( method=RequestMethod.DELETE) 用于删除数据
@RestController: 符合注解, 是@Controller 和@ResponseBody组合。
四、具体怎么实现呢?
1、配置
(针对springboot,ssm的话需要各位自己去搜它的配置)
在application.properties(yml)里面添加
#支持put,delete的启动
spring.mvc.hiddenmethod.filter.enabled=true
2、前端
<h3>测试rest请求方式</h3>
<form action="student/test" method="post">
<input type="hidden" name="_method" value="put">
<input type="submit" value="测试请求方式">
</form>
这里跟以前不一样在于 多了一行 <input type="hidden" name="_method" value="put">,因为我们的method只有post和get,如果我们想使用put来和控制层的@PutMapping匹配,就要这样用
3、后端
package com.bjpowernode.controller;
import org.springframework.web.bind.annotation.*;
@RestController
public class MyRestController {
@PutMapping("/student/test")
public String test(){
return "执行了/student/test,使用put请求";
}
}
四、完整代码(动力节点版)
1、整体结构
2、MyRestController
package com.bjpowernode.controller;
import org.springframework.web.bind.annotation.*;
@RestController
public class MyRestController {
// 学习注解的使用
//查询id=1001的学生
/**
* @PathVariable(路径变量) : 获取url中的数据
* 属性: value : 路径变量名
* 位置: 放在控制器方法的形参前面
*
* http://localhost:8080/myboot/student/1002
*
* {stuId}:定义路径变量, stuId自定义名称
*/
@GetMapping("/student/{stuId}")
public String queryStudent(@PathVariable("stuId") Integer studentId){
return "查询学生studentId="+studentId;
}
/***
* 创建资源 Post请求方式
* http://localhost:8080/myboot/student/zhangsan/20
*/
@PostMapping("/student/{name}/{age}")
public String createStudent(@PathVariable("name") String name,
@PathVariable("age") Integer age){
return "创建资源 student: name="+name+"#age="+age;
}
/**
* 更新资源
*
* 当路径变量名称和 形参名一样, @PathVariable中的value可以省略
*/
@PutMapping("/student/{id}/{age}")
public String modifyStudent(@PathVariable Integer id,
@PathVariable Integer age){
return "更新资源, 执行put请求方式: id="+id+"#age="+age;
}
/**
* 删除资源
*/
@DeleteMapping("/student/{id}")
public String removeStudentById(@PathVariable Integer id){
return "删除资源,执行delete, id="+id;
}
@PutMapping("/student/test")
public String test(){
return "执行了/student/test,使用put请求";
}
@DeleteMapping("/student/DeleteTest")
public String DeleteTest(){
return "执行了/student/DeleteTest,使用delete请求";
}
}
3、application.properties
server.port=9001
server.servlet.context-path=/myboot
#支持put,delete的启动
spring.mvc.hiddenmethod.filter.enabled=true
4、addStudent.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>添加学生</h3>
<form action="student/zhangsan/20" method="post">
<input type="submit" value="注册学生">
</form>
</body>
</html>
5、testrest.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>测试rest请求方式</h3>
<form action="student/test" method="post">
<input type="hidden" name="_method" value="put">
<input type="submit" value="测试请求方式">
</form>
<form action="student/DeleteTest" method="post">
<input type="hidden" name="_method" value="delete">
<input type="submit" value="删除方式">
</form>
</body>
</html>
我只是看了这个视频后把源码和自己的思路总结一下,大家有兴趣的话可以看一下原视频