当前位置:网站首页>07_SpingBoot 实现 RESTful 风格
07_SpingBoot 实现 RESTful 风格
2022-06-24 19:41:00 【书启秋枫】
一、认识 RESTFul
REST(英文:Representational State Transfer,简称REST)
一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次,REST这个词,是Roy Thomas Fielding在他2000年的博士论文中提出的。
以前:访问资源(图片,servlet程序),请求资源同时带上请求方式,如果get请求直接访问到doget方法上,如果post请求直接访问到dopost
rest理念访问资源:请求资源,然后按照请求的方式进行处理,如果是get方式,查询操作,如果是put方式 更新操作,如果是delete方式 删除资源,如果是post方式 添加资源。
任何的技术都可以实现这种rest理念,如果一个架构符合REST原则,就称它为RESTFul架构
外在体现:
比如我们要访问一个http接口:http://localhost:8080/boot/order?id=1021&status=1
质询参数?id=1021&status=1
采用RESTFul风格则http地址为:http://localhost:8080/boot/order/1021/1
二、Spring Boot 开发 RESTFul
Spring boot开发RESTFul 主要是几个注解实现
1. @PathVariable
获取url中的数据
该注解是实现RESTFul最主要的一个注解
2. @PostMapping
接收和处理Post方式的请求
3. @DeleteMapping
接收delete方式的请求,可以使用GetMapping代替
4. @PutMapping
接收put方式的请求,可以用PostMapping代替
5. @GetMapping
接收get方式的请求
三、RESTful的优点
- 轻量,直接基于http,不再需要任何别的诸如消息协议
get/post/put/delete为CRUD操作
- 面向资源,一目了然,具有自解释性。
- 数据描述简单,一般以xml,json做数据交换。
- 无状态,在调用一个接口(访问、操作资源)的时候,可以不用考虑上下文,不用考虑当前状态,极大的降低了复杂度。
- 简单、低耦合
四、使用RESTful风格模拟实现对学生的增删改查操作
1. 创建RESTfulController,并编写代码
@RestController
public class RESTfulController {
/*
* 添加学生
* 请求地址:http://localhost:9094/004-springboot-springmvc/springBoot/student/suke/119
* 请求方式:POST
* @param name
* @param age
* @return
* */
@PostMapping(value = "/springBoot/student/{name}/{age}")
public Object addStudent(@PathVariable("name") String name,
@PathVariable("age") Integer age) {
Map<String, Object> retMap = new HashMap<String, Object>();
retMap.put("name", name);
retMap.put("age", age);
return retMap;
}
/*
* 删除学生
* 请求地址:http://localhost:9094/004-springboot-springmvc/springBoot/student/110
* 请求方式:Delete
*
* @param id
* @return
* */
@DeleteMapping(value = "/springBoot/student/{id}")
public Object removeStudent(@PathVariable("id") Integer id) {
return "删除的学生id为:" + id;
}
/*
* 修改学生信息
* 请求地址:http://localhost:9094/004-springboot-springmvc/springBoot/student/120
* 请求方式:Put
*
* @param id
* @return
* */
@PutMapping(value = "/springBoot/student/{id}")
public Object modifyStudent(@PathVariable("id") Integer id) {
return "修改学生的id为" + id;
}
@GetMapping(value = "/springBoot/student/{id}")
public Object queryStudent(@PathVariable("id") Integer id) {
return "查询学生的id为" + id;
}
}2. 使用Postman模拟发送请求,进行测试
(1)@PostMapping(value = "/springBoot/student/{name}/{age}")
添加学生
请求地址:http://localhost:9004/004-springboot-springmvc/springBoot/student/ls/119
请求方式:POST

(2)@DeleteMapping(value = "/springBoot/student/{id}")
删除学生
请求地址:http://localhost:9004/004-springboot-springmvc/springBoot/student/110
请求方式:Delete

(3)@PutMapping(value = "/springBoot/student/{id}")
修改学生信息
请求地址:http://localhost:9004/004-springboot-springmvc/springBoot/student/120
请求方式:Put

(4)@GetMapping(value = "/springBoot/student/{id}")
查询学生信息
请求地址:http://localhost:9004/004-springboot-springmvc/springBoot/student/123
请求方式:Get

(5)总结:其实这里我们能感受到的好处
传递参数变简单了
服务提供者对外只提供了一个接口服务,而不是传统的CRUD四个接口
3. 请求冲突的问题
- 改路径
- 改请求方式
(1)创建RESTfulController类
/*
* queryOrder1和queryOrder2两个请求路径会发生请求路径冲突问题
* queryOrder3与queryOrder1和queryOrder2发生请求不冲突
* 注意:虽然两个路径写法改变了,但是由于传递的两个参数都是int值,所以不知道该交给哪个请求进行处理
* 就会出现匹配模糊不清的异常,所以要想解决冲突,有两种方式:
* 1.修改请求路径
* 2.修改请求方式
* */
@RestController
public class RESTfulController2 {
@GetMapping(value = "/springBoot/order/{id}/{status}")
public Object queryOrder(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
System.out.println("-----queryOrder--------");
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("status", status);
return map;
}
@GetMapping(value = "/springBoot/{id}/order/{status}")
public Object queryOrder1(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
System.out.println("-----queryOrder1--------");
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("status", status);
return map;
}
@GetMapping(value = "/springBoot/{status}/order/{id}")
public Object queryOrder2(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("status", status);
return map;
}
@PostMapping(value = "/springBoot/{status}/order/{id}")
public Object queryOrder3(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", id);
map.put("status", status);
return map;
}
}(2)结合Postman进行测试说明
@GetMapping(value = "/springBoot/order/{id}/{status}")
解决:改路径
@GetMapping(value = "/springBoot/{id}/order/{status}")
@GetMapping(value = "/springBoot/{status}/order/{id}")
报错:出现请求冲突
@PostMapping(value = "/springBoot/{status}/order/{id}")
解决:改post请求方式
五、RESTful原则
① 增post请求、删delete请求、改put请求、查get请求
② 请求路径不要出现动词
例如:查询订单接口
/boot/order/1021/1(推荐)
/boot/queryOrder/1021/1(不推荐)
User/name/age/sex? page=1&sort=desc
③ 分页、排序等操作,不需要使用斜杠传参数
例如:订单列表接口
/boot/orders?page=1&sort=desc
一般传的参数不是数据库表的字段,可以不采用斜杠
④ REST:
- 请求资源
- 请求方式
- 根据参数操作
- 不是资源的信息(参数),一般不用斜杠传参数,采用质询参数
边栏推荐
- win10或win11打印机无法打印
- Rip protocol of dynamic routing protocol
- 详细了解Redis的八种数据类型及应用场景分析
- China solar window market trend report, technical dynamic innovation and market forecast
- Combine pod identity in aks and secret in CSI driver mount key vault
- Research Report on market supply and demand and strategy of ceiling power supply device industry in China
- Extend your kubernetes API with aggregated apiserver
- Memory alignment of structures
- 2022年高处安装、维护、拆除考试模拟100题及模拟考试
- 研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
猜你喜欢

docker安装mysql-简单无坑

vulnhub Vegeta: 1

Introduction to machine learning compilation course learning notes lesson 1 overview of machine learning compilation

Database transaction Transanction

Talk about GC mechanism often asked in interview

Tetris

Memory alignment of structures

Combine pod identity in aks and secret in CSI driver mount key vault

糖豆人登录报错解决方案

详细了解关于sentinel的实际应用
随机推荐
Wechat side: what is consistent hash? In what scenario? What problems have been solved?
High level application of SQL statements in MySQL database (I)
Extend your kubernetes API with aggregated apiserver
Database transaction Transanction
Feign project construction
Talk about GC mechanism often asked in interview
关于某手滑块的一些更新(6-18,js逆向)
动态菜单,自动对齐
剑指 Offer 42. 连续子数组的最大和
MySQL + JSON = King fried!!
Source code reading | the process of reading text format STL by openmesh
New, Huawei cloud Kaitian apaas
Solve the problem of non secure websites requesting localhost to report CORS after chrome94
研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
Servlet
ThreadLocal memory leak
LeetCode Algorithm 剑指 Offer II 027. 回文链表
JMM 最最最核心的概念:Happens-before 原则
Fanuc robot_ Introduction to Karel programming (1)
Firewall working principle and detailed conversation table


