Spring Boot下的Spring MVC和之前的Spring MVC使用是完全一样的,主要有以下注解
- @Controller:Spring MVC的注解,处理 http 请求
@RestControlle:Spring 4 后新增注解,是 @Controller 和 @ResposeBody 的组合注解
@ResponseBody:用于返回字符串或 json 数据
@RequestMapping:支持 Get 和 Post 请求
@GetMapping: RequestMapping 和 Get 请求的组合,只支持 Get 请求,Get 请求主要用于查询操作
@PostMapping:~,请求主要用于用户新增数据
- @PutMapping:~,用于修改数据
@DeleteMapping:~,用于删除数据
综合案例:
①创建一个MVCController,里面使用上面介绍的各种注解接收不同的请求
//RestController注解相当于加了给方法加了@ResponseBody注解,所以是不能跳转页面的,只能返回字符串或者json数据
@RestController
public class MVCController {
@RequestMapping(value="/boot/req",method = {RequestMethod.GET,RequestMethod.POST})
public Object req(){
return "req";
}
@GetMapping("/boot/get")
public Object get(){
return "get";
}
@PostMapping("/boot/post")
public Object post(){
return "post";
}
@PutMapping("/boot/put")
public Object put(){
return "put";
}
@DeleteMapping("/boot/delete")
public Object delete(){
return "delete";
}
}
②Http接口请求工具Postman介绍
作用:使用 Postman 进行其它请求类型测试
因为通过浏览器输入地址,默认发送的只能是get请求,通过Postman工具,可以模拟发送不同类型的请求,并查询结果,在安装的时候,有些机器可能会需要安装MicroSort .NET framework
2、SpringBoot 实现 Restful
①Restfu简介
REST:Representational State Transfer
- 一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次,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
②Spring Boot 开发 RESTFUL
主要注解实现:
- @PathVariable:获取url中的数据;该注解是实现RESTFul最主要的一个注解
@PostMapping:接收和处理Post方式的请求
@DeleteMapping:接收delete方式的请求,可以使用GetMapping代替
@PutMapping:接收put方式的请求,可以用PostMapping代替
@GetMapping:接收get方式的请求
3、案例:使用RESTful风格模拟实现对学生的增删改查操作③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模拟发送请求,进行测试
4、请求冲突小结:
- 传递参数变简单了
- 服务提供者对外只提供了一个接口服务,而不是传统的CRUD四个接口
解决办法:
- 改路径
- 改请求方式
@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;
}
}
5、RESTFUL原则
- 增post请求、删delete请求、改put请求、查get请求
- 请求路径不要出现动词
例如:查询订单接口
/boot/order/1021/1(推荐)
/boot/queryOrder/1021/1(不推荐)
- 分页、排序等操作,不需要使用斜杠传参数
例如:订单列表接口
/boot/orders?page=1&sort=desc
一般传的参数不是数据库表的字段,可以不采用斜杠
***REST:
- 请求资源
- 请求方式
- 根据参数操作
- 不是资源的信息(参数),一般不用斜杠传参数,采用质询参数



