SpringMVC常用注解
@RestController
表明所有方法都返回json对象,之前要返回一个json对象的时候,我们的做法是在方法上@ResponseBody,然后返回一个对象,让框架用jackson帮我们转成json格式。现在如果整个类中的方法返回的都是json格式,就可以直接在类上添加这个注解。
@RestController
public class StudentController {
@RequestMapping("/getStudent")
public Object getStudentById(Integer id) {
Student student = new Student();
student.setId(id);
student.setName("zhangsan");
student.setAge(22);
return student;
}
}
然后在postman中验证一下。postman是一个比较好用的工具,比浏览器更方便,可以控制请求的类型。下载地址,教程。
![](https://cdn.jsdelivr.net/gh//Fushier/ImgCloud@main/data/20210123111417.png)
@xxxMaping
之前在SpringMVC中说过,如果@RequestMapping中不添加属性method,就等于不限制请求的方法。如果加了method = RequestMethod.POST,就只能用post请求。而注解**@PostMapping和这种写法是等价的**,而且更直观明了。
之前我们验证的时候需要专门写一个jsp页面,用form进行post请求,现在就不用这么麻烦了,在postman中可以选择请求的方式。
// @RequestMapping(value = "/postStudent", method = RequestMethod.POST)
@PostMapping("/postStudent")
public Object postStudent() {
return "post method";
}
![](https://cdn.jsdelivr.net/gh//Fushier/ImgCloud@main/data/20210123112231.png)
现在改为POST请求,再试一下:
![](https://cdn.jsdelivr.net/gh//Fushier/ImgCloud@main/data/20210123112338.png)
类似的注解还有@GetMapping,只支持GET请求;@DeleteMapping,只支持DELETE请求;@PutMapping,只支持PUT请求。这四种注解对应了数据的增删查改。在RESTful风格中,这四种方法就显得比较重要了,而不是像之前只用GET和POST。
REST
REST(Representational State Transfer),一种互联网软件架构设计的风格,而不是标准。它提出了一组客户端和服务器交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更加简洁,更有层次。任何的技术都可以实现这种理念,如果一个架构符合REST原则,就成它为RESTful架构。
比如要访问一个http接口:http://localhost:8080/boot/order?id=1001&status=1
采用RESTful风格,则http地址为:http://localhost:8080/boot/order/1001/1
RESTful原则:
-
增post请求,删delete请求,改put请求,查get请求
-
请求路径不要出现动词。比如查询订单接口 /boot/order/1021/1 (推荐) /boot/queryOrder/1021/1 (不推荐)
-
分页、排序等操作,不需要使用斜杠传参数
例如:订单列表接口 /boot/orders?page=1&sort=desc
一般传的参数不是数据库表的字段,可以不采用斜杠
-
名词分割不要用下划线 “_”,而是用 “-”
这一块其实单看理论还是有点懵的,个人也查看了很多博客,看到知乎这个回答有点茅塞顿开的感觉:
- 看Url就知道要什么
- 看http method就知道干什么
- 看http status code就知道结果如何
回想一下之前写javaweb的时候,要做什么都在URL上体现了,比如查询学生就是/queryStudent?id=1001,或者id不在url上而是在header/content里,后端有一个处理方法与之关联@RequestMapping("/queryStudent")。如果新增一个学生,就用form提交数据,再写一个处理方法@RequestMapping("/insertStudent")。但是URL是URI的子集,它原本应该是资源定位符,就是用来定位资源的,现在的写法http://localhost:8080/insertStudent更应该说是一种动作,HTTP 1.1之后加入的新请求我们压根用不上。
那么REST风格是怎样的?其实就是上面提到的三点,URL只用来表示资源,具体的动作用HTTP请求去表示,假如现在有一个URL:http://localhost:8080/students/1001 那它只是表示id是1001的这个学生,至于你到底要对这个资源干什么,是更新他?还是删除他?还是查询?就根据你用的到底是PUT,还是DELETE,还是GET来确定了,这就要到上面提到那四个注解了。
Spring Boot开发RESTful主要是通过@PathVariable实现:
比如现在还是要getStudentById,RESTful风格写法应该是这样的:
@GetMapping("/students/{id}")
public Object getStudentById(@PathVariable("id") Integer id) {
Student student = new Student();
student.setId(id);
student.setName("zhangsan");
student.setAge(22);
return student;
}
用postman再验证一下:
![](https://cdn.jsdelivr.net/gh//Fushier/ImgCloud@main/data/20210123142142.png)
那deleStudentById的写法也就非常明确了:
@DeleteMapping("/students/{id}")
public String deleteStudentById(@PathVariable("id") Integer id) {
return "学生:" + id + "删除成功";
}
![](https://cdn.jsdelivr.net/gh//Fushier/ImgCloud@main/data/20210123142452.png)
Spring Boot传递参数
之前SpringMVC讲的有点不太全,只讲了通过form传递参数,其实还可以通过content传递参数,设置content-type为application/json,也可以传递对象参数。特别详细的传参数的可以看这个网址,讲的挺全的。
先复习一下通过form传递对象:
@PostMapping("/students")
public String postStudent(Student student) {
return student.toString();
}
![](https://cdn.jsdelivr.net/gh//Fushier/ImgCloud@main/data/20210123145039.png)
传递多个对象的时候肯定不能用表单了,所以用json还是更方便一些,区别只是在参数上加一个@RequestBody:
@PostMapping("/students")
public String postStudents(@RequestBody List<Student> students) {
return students.toString();
}
![](https://cdn.jsdelivr.net/gh//Fushier/ImgCloud@main/data/20210123145637.png)