[SpringMVC]REST入门案例与优化

网友投稿 306 2022-09-05

[SpringMVC]REST入门案例与优化

文章目录

​​REST入门案例​​

​​案例需求与思路分析​​​​环境准备​​​​修改REST风格​​

​​新增​​​​删除​​​​传递路径参数​​​​修改​​​​根据ID查询​​​​查询所有​​

​​案例总结​​

​​@PathVariable​​

​​RESTful快速开发​​​​快速开发总结​​

​​知识点1:@RestController​​​​知识点2:@GetMapping @PostMapping @PutMapping @DeleteMapping​​

REST入门案例

案例需求与思路分析

需求:将之前的增删改查替换成RESTful的开发方式。

1.之前不同的请求有不同的路径,现在要将其修改为统一的请求路径

修改前: 新增: /save ,修改: /update,删除 /delete…

修改后: 增删改查: /users

2.根据GET查询、POST新增、PUT修改、DELETE删除对方法的请求方式进行限定

3.发送请求的过程中如何设置请求参数?

环境准备

创建一个Web的Maven项目pom.xml添加Spring依赖

编写模型类User

public class User { private String name; private int age; //getter...setter...toString省略}

编写UserController和BookController

@Controllerpublic class UserController { @RequestMapping("/save") @ResponseBody public String save(@RequestBody User user) { System.out.println("user save..."+user); return "{'module':'user save'}"; } @RequestMapping("/delete") @ResponseBody public String delete(Integer id) { System.out.println("user delete..." + id); return "{'module':'user delete'}"; } @RequestMapping("/update") @ResponseBody public String update(@RequestBody User user) { System.out.println("user update..." + user); return "{'module':'user update'}"; } @RequestMapping("/getById") @ResponseBody public String getById(Integer id) { System.out.println("user getById..." + id); return "{'module':'user getById'}"; } @RequestMapping("/findAll") @ResponseBody public String getAll() { System.out.println("user getAll..."); return "{'module':'user getAll'}"; }}

修改REST风格

现在我们将UserController进行改造:

新增

@Controllerpublic class UserController { //设置当前请求方法为POST,表示REST风格中的添加操作 @RequestMapping(value = "/users",method = RequestMethod.POST) @ResponseBody public String save() { System.out.println("user save..."); return "{'module':'user save'}"; }}

将请求路径更改为​​/users​​

访问该方法使用 POST:​​class UserController { //设置当前请求方法为DELETE,表示REST风格中的删除操作 @RequestMapping(value = "/users",method = RequestMethod.DELETE) @ResponseBody public String delete(Integer id) { System.out.println("user delete..." + id); return "{'module':'user delete'}"; }}

将请求路径更改为​​/users​​

访问该方法使用 DELETE:​​class UserController { //设置当前请求方法为DELETE,表示REST风格中的删除操作 @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE) @ResponseBody public String delete(@PathVariable Integer id) { System.out.println("user delete..." + id); return "{'module':'user delete'}"; }}

思考如下两个问题:

(1)如果方法形参的名称和路径​​{}​​中的值不一致,该怎么办?

(2)如果有多个参数需要传递该如何编写?

前端发送请求的时候使用:​​class UserController { //设置当前请求方法为DELETE,表示REST风格中的删除操作 @RequestMapping(value = "/users/{id}/{name}",method = RequestMethod.DELETE) @ResponseBody public String delete(@PathVariable Integer id,@PathVariable String name) { System.out.println("user delete..." + id+","+name); return "{'module':'user delete'}"; }}

修改

@Controllerpublic class UserController { //设置当前请求方法为PUT,表示REST风格中的修改操作 @RequestMapping(value = "/users",method = RequestMethod.PUT) @ResponseBody public String update(@RequestBody User user) { System.out.println("user update..." + user); return "{'module':'user update'}"; }}

将请求路径更改为​​/users​​

访问该方法使用 PUT:​​class UserController { //设置当前请求方法为GET,表示REST风格中的查询操作 @RequestMapping(value = "/users/{id}" ,method = RequestMethod.GET) @ResponseBody public String getById(@PathVariable Integer id){ System.out.println("user getById..."+id); return "{'module':'user getById'}"; }}

将请求路径更改为​​/users​​

访问该方法使用 GET:​​class UserController { //设置当前请求方法为GET,表示REST风格中的查询操作 @RequestMapping(value = "/users" ,method = RequestMethod.GET) @ResponseBody public String getAll() { System.out.println("user getAll..."); return "{'module':'user getAll'}"; }}

将请求路径更改为​​/users​​

访问该方法使用 GET:​​= RequestMethod.POST|GET|PUT|DELETE)

(2)设定请求参数(路径变量)

@RequestMapping(value=“/users/{id}”,method = RequestMethod.DELETE)

@ReponseBody

public String delete(@PathVariable Integer id){

}

@PathVariable

名称

@PathVariable

类型

形参注解

位置

SpringMVC控制器方法形参定义前面

作用

绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应

关于接收参数,我们学过三个注解​​@RequestBody​​​、​​@RequestParam​​​、​​@PathVariable​​,这三个注解之间的区别和应用分别是什么?

区别

@RequestParam用于接收url地址传参或表单传参@RequestBody用于接收json数据@PathVariable用于接收路径参数,使用{参数名称}描述路径参数

应用

后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广如果发送非json格式数据,选用@RequestParam接收请求参数采用RESTful进行开发,当参数数量较少时,例如1个,可以采用@PathVariable接收请求路径变量,通常用于传递id值

RESTful快速开发

做完了RESTful的开发,你会发现好麻烦,麻烦在哪?

问题1:每个方法的@RequestMapping注解中都定义了访问路径/books,重复性太高。

问题2:每个方法的@RequestMapping注解中都要使用method属性定义请求方式,重复性太高。

问题3:每个方法响应json都需要加上@ResponseBody注解,重复性太高。

对于上面所提的这三个问题,具体该如何解决?

@RestController //@Controller + ReponseBody@RequestMapping("/books")public class BookController { //@RequestMapping(method = RequestMethod.POST) @PostMapping public String save(@RequestBody Book book){ System.out.println("book save..." + book); return "{'module':'book save'}"; } //@RequestMapping(value = "/{id}",method = RequestMethod.DELETE) @DeleteMapping("/{id}") public String delete(@PathVariable Integer id){ System.out.println("book delete..." + id); return "{'module':'book delete'}"; } //@RequestMapping(method = RequestMethod.PUT) @PutMapping public String update(@RequestBody Book book){ System.out.println("book update..." + book); return "{'module':'book update'}"; } //@RequestMapping(value = "/{id}",method = RequestMethod.GET) @GetMapping("/{id}") public String getById(@PathVariable Integer id){ System.out.println("book getById..." + id); return "{'module':'book getById'}"; } //@RequestMapping(method = RequestMethod.GET) @GetMapping public String getAll(){ System.out.println("book getAll..."); return "{'module':'book getAll'}"; } }

对于刚才的问题,我们都有对应的解决方案:

问题1:每个方法的@RequestMapping注解中都定义了访问路径/books,重复性太高。

将@RequestMapping提到类上面,用来定义所有方法共同的访问路径。

问题2:每个方法的@RequestMapping注解中都要使用method属性定义请求方式,重复性太高。

使用@GetMapping @PostMapping @PutMapping @DeleteMapping代替

问题3:每个方法响应json都需要加上@ResponseBody注解,重复性太高。

1.将ResponseBody提到类上面,让所有的方法都有@ResponseBody的功能2.使用@RestController注解替换@Controller与@ResponseBody注解,简化书写

快速开发总结

知识点1:@RestController

名称

@RestController

类型

类注解

位置

基于SpringMVC的RESTful开发控制器类定义上方

作用

设置当前控制器类为RESTful风格,

等同于@Controller与@ResponseBody两个注解组合功能

知识点2:@GetMapping @PostMapping @PutMapping @DeleteMapping

名称

@GetMapping @PostMapping @PutMapping @DeleteMapping

类型

方法注解

位置

基于SpringMVC的RESTful开发控制器方法定义上方

作用

设置当前控制器方法请求访问路径与请求动作,每种对应一个请求动作,

例如@GetMapping对应GET请求

相关属性

value(默认):请求访问路径

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[Spring Framework]DI依赖注入①
下一篇:DoMarketing-营销智库:告诉你一个真实的新消费!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~