这是我参与「第四届青训营 」笔记创作活动的第5天。
目前前后端分离的开发模式成为主流,如何定义好接口显得尤为重要。因此RestFul风格接口设计诞生了。与此同时,java后端springboot框架的controller层用来定义接口。让我们来学习前端如何定义接口以及后端怎么编写接口。
RestFul风格
什么是RestFul风格
Restful是一个资源定位、资源操作的风格。不是一种标准也不是一种协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
资源:互联网所有的事物都可以被抽象为资源。 资源操作:分为POST、DELETE、PUT、GET四种方法,使用不同方法对资源进行操作(增、删、改、查)
Rest架构主要原则
- 对网络上所有的资源都有一个资源标志符。
- 对资源的操作不会改变标识符。
- 同一资源有多种表现形式(xml、json)
- 所有操作都是无状态的(Stateless)
传统风格与RestFul风格对比
传统URL请求格式:
http://127.0.0.1/user/select/1 GET 根据用户id查询用户数据
http://127.0.0.1/user/insert POST 新增用户
http://127.0.0.1/user/update POST 修改用户信息
http://127.0.0.1/user/delete GET/POST 删除用户信息
RESTful请求格式:
http://127.0.0.1/user/1 GET 根据用户id查询用户数据
http://127.0.0.1/user POST 新增用户
http://127.0.0.1/user PUT 修改用户信息
http://127.0.0.1/user DELETE 删除用户信息
由上可以看出,传统设计风格使用语义明显的动词区分不同的操作,而RestFul风格对于同一个对象URL相同,只是http提交的请求方式不同。
springboot使用RestFul风格
springboot框架中,在controller层中编写接口,使用注解可以快速开发具有RestFul特性的接口,如下所示:
@RestController
@RequestMapping(value = "/do")
public class CarlotController {
@Autowired
private CarlotService carlotService;
/*添加车位*/
@RequestMapping(value = "/carlot" ,method = RequestMethod.POST)
public boolean addCarlot(@RequestBody Carlot carlot){
return carlotService.addCarlot(carlot);
}
/*查询所有车位*/
@RequestMapping(value = "/carlot",method = RequestMethod.GET)
public List<Carlot> findAll(){
return carlotService.findAll();
}
@RequestMapping(value = "/carlot",method = RequestMethod.PUT)
public boolean updateCarlot(@RequestBody Carlot carlot){
return carlotService.updateCarlot(carlot);
}
@RequestMapping(value = "/carlot",method = RequestMethod.DELETE)
public boolean deleteCarlot(@RequestParam(value = "id",required = true) String id){
return carlotService.deleteCarlot(id);
}
}
@RestController
这个注解会把所有的返回结果以json的形式进行返回。
@RequestMapping
用于标识此controller请求地址路径,比如上面@RequestMapping("value=/do"),代表需要通过“/do”才能访问这个接口,这个注解通常同时放到类与方法上,表示一个模块的各个部分。设置method参数可以限定访问方式,同时也有相应的注解简化代码:
@PostMapping(value = "/carlot")
public boolean addCarlot(@RequestBody Carlot carlot){
return carlotService.addCarlot(carlot);
}
/*查询所有车位*/
@GetMapping(value = "/carlot")
public List<Carlot> findAll(){
return carlotService.findAll();
}
@PutMapping(value = "/carlot")
public boolean updateCarlot(@RequestBody Carlot carlot){
return carlotService.updateCarlot(carlot);
}
@DeleteMapping(value = "/carlot")
public boolean deleteCarlot(@RequestParam(value = "id",required = true) String id){
return carlotService.deleteCarlot(id);
}
参数传递
可以直接在方法参数中使用相同的变量名接收参数,如下:
// 假设接口为 /carlot?id=1
@DeleteMapping(value = "/carlot")
public boolean deleteCarlot(String id){
return carlotService.deleteCarlot(id);
}
如果参数太多,也可以使用bean对象接收,前提是bean对象属性名要和参数名相同,同时要有get和set方法,且没有传值的参数为null
// 假设接口为 /carlot?id=1&age=12
@DeleteMapping(value = "/carlot")
public boolean deleteCarlot(User user){
return carlotService.deleteCarlot(user);
}
public class User {
private String name;
private Integer age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@RequestBody接收json传参 http请求类型一般为application/json,根据json的key与bean对象属性值进行一一匹配。
@PostMapping(value = "/carlot")
public boolean addCarlot(@RequestBody Carlot carlot){
return carlotService.addCarlot(carlot);
}
@RequestParam可以限定参数是否必须 正常情况下此参数是可以省略的。但是当这个参数是必填项,或者有默认值,改注解不省略。
@DeleteMapping(value = "/carlot")
public boolean deleteCarlot(@RequestParam(value = "id",required = true,defaultValue="123") String id){
return carlotService.deleteCarlot(id);
}
@PathVariable 它是以“/”方式来获取参数值。是从一个url模板里面来填充(绑定 URL占位符到功能处理方法的参数上,主要实现RESTFULL风格的请求。如接口 /carlot/{id}
@DeleteMapping(value = "/carlot/{id}")
public boolean deleteCarlot(@PathVariable("id") String id){
return carlotService.deleteCarlot(id);
}
总结
RestFul设计风格可以让前后端设计人员保持一定的一致性,提高编码效率。同时springboot的controller层注解开发也很好的配合了RestFul风格设计。