当前位置:网站首页>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:
- 请求资源
- 请求方式
- 根据参数操作
- 不是资源的信息(参数),一般不用斜杠传参数,采用质询参数
边栏推荐
- cat写多行内容到文件
- Based on the codeless platform, users deeply participated in the construction, and digital data + Nanjing Fiberglass Institute jointly built a national smart laboratory solution
- Research and investment strategy report on China's bridge anticorrosive coating industry (2022 Edition)
- 【文本数据挖掘】中文命名实体识别:HMM模型+BiLSTM_CRF模型(Pytorch)【调研与实验分析】
- CDN principle
- Servlet
- JWT(Json Web Token)
- Recommended course: workplace writing training
- ACL (access control list) basic chapter - Super interesting learning network
- Problem solving - nested lists
猜你喜欢
vulnhub Vegeta: 1
[text data mining] Chinese named entity recognition: HMM model +bilstm_ CRF model (pytoch) [research and experimental analysis]
C#学习两年的增删改查和C#导入导出(去重)案例
Web security XSS foundation 06
Talk about GC mechanism often asked in interview
What kind of processor architecture is ARM architecture?
Annotation
Heavyweight! Fada is listed as a "specialized and new" enterprise
[Wuhan University] information sharing of the first and second postgraduate entrance examinations
Combine pod identity in aks and secret in CSI driver mount key vault
随机推荐
CDN principle
China smallpox vaccine market trend report, technical innovation and market forecast
See how sparksql supports enterprise level data warehouse
Research Report on market supply and demand and strategy of China's solar charging controller industry
【Laravel系列7.9】测试
2022-06-10 工作记录--JS-获取到某一日期N天后的日期
关于某手滑块的一些更新(6-18,js逆向)
Feign project construction
ThreadLocal local thread
Solve the problem of port occupation
花房集团二次IPO:成于花椒,困于花椒
Solution to the login error of tangdou people
docker安装mysql-简单无坑
Analyze the implementation process of oauth2 distributed authentication and authorization based on the source code
Servlet
Annotation
How to submit the shopee opening and settlement flow?
环境配置 | VS2017配置OpenMesh源码和环境
docker安装redis-简单而无坑
canvas 实现图片新增水印