2、Spring Boot开发RESTFul
- REST(英文:Representational State Transfer,简称REST)
- 一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次,REST这个词,是Roy Thomas Fielding在他2000年的博士论文中提出的。
- ***以前:访问资源(图片,servlet程序),请求资源同时带上请求方式,如果get请求直接访问到doget方法上,如果post请求直接访问到dopost
- ***rest理念访问资源:请求资源,然后按照请求的方式进行处理,如果说get方式,查询操作,如果put 更新操作,如果是delete方式 删除资源,如果是post方式 添加资源
- 任何的技术都可以实现这种理念,如果一个架构符合REST原则,就称它为RESTFul架构
- 外在体现:
- 比如我们要访问一个http接口:http://localhost:8080/boot/order?id=1021&status=1
- 采用RESTFul风格则http地址为:http://localhost:8080/boot/order/1021/1
3、RestFul 风格的优点Spring boot开发RESTFul 主要是几个注解实现:
- @PathVariable:获取url中的数据,该注解是实现RESTFul最主要的一个注解
- @PostMapping:接收和处理Post方式的请求
@DeleteMapping:接收delete方式的请求,可以使用GetMapping代替
- @PutMapping:接收put方式的请求,可以用PostMapping代替
@GetMapping:接收get方式的请求
4、案例:使用RESTful风格模拟实现对学生的增删改查操作
- 轻量,直接基于http,不再需要任何别的诸如消息协议
get/post/put/delete为CRUD操作
- 面向资源,一目了然,具有自解释性。
- 数据描述简单,一般以xml,json做数据交换。
- 无状态,在调用一个接口(访问、操作资源)的时候,可以不用考虑上下文,不用考虑当前状态,极大的降低了复杂度。
- 简单、低耦合
①创建RESTfulController,并编写代码
@RestController
public class RESTfulController {
@PostMapping(value = "/springBoot/student/{name}/{age}")
public Object addStudent(@PathVariable("name") String name,
@PathVariable("age") Integer age) {
Map retMap = new HashMap();
retMap.put("name",name);
retMap.put("age",age);
return retMap;
}
@DeleteMapping(value = "/springBoot/student/{id}")
public Object removeStudent(@PathVariable("id") Integer id) {
return "删除的学生id为:" + id;
}
@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;
}
}
②使用Postman模拟发送请求,进行测试
5、请求冲突问题小结:
- 传递参数变简单了
- 服务提供者对外只提供了一个接口服务,而不是传统的CRUD四个接口
- 创建RESTfulController类,结合Postman进行测试说明
@RestController
public class RESTfulController {
@GetMapping(value = "/springBoot/order/{id}/{status}")
public Object queryOrder(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
Map map = new HashMap();
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) {
Map map = new HashMap();
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 map = new HashMap();
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 map = new HashMap();
map.put("id",id);
map.put("status",status);
return map;
}
}
6、RESTFUL 原则
- 增post请求、删delete请求、改put请求、查get请求
- 请求路径不要出现动词
例如:查询订单接口
/boot/order/1021/1(推荐)
/boot/queryOrder/1021/1(不推荐)
- 分页、排序等操作,不需要使用斜杠传参数
例如:订单列表接口
/boot/orders?page=1&sort=desc
一般传的参数不是数据库表的字段,可以不采用斜杠
***REST:
- 请求资源
- 请求方式
- 根据参数操作
- 不是资源的信息(参数),一般不用斜杠传参数,采用质询参数



