一文解读如何应用 REST 对资源进行访问?

文章目录

  • [一、REST 简介](#一、REST 简介)
  • 二、涉及注解
    • [2.1 @RequestMapping](#2.1 @RequestMapping)
    • [2.2 @PathVariable](#2.2 @PathVariable)
    • [2.3 @RestController](#2.3 @RestController)
    • [2.4 @GetMapping、@PostMapping、@PutMapping、@DeleteMapping](#2.4 @GetMapping、@PostMapping、@PutMapping、@DeleteMapping)
    • [补充:@PathVariable、@RequestBody、@RequestParam 区别与应用](#补充:@PathVariable、@RequestBody、@RequestParam 区别与应用)
  • 三、REST风格案例

一、REST 简介

REST (Representational State Transfer) ,表现形式状态转换。

描述 传统风格 REST风格
查询用户 http://localhost/user/getById?id=1 http://localhost/user/1
保存用户 http://localhost/user/saveUser http://localhost/user

优点:书写简化;隐藏资源的访问行为,无法通过地址得知对资源是何种操作

按照 REST 风格访问资源时使用 行为动作 区分对资源进行了何种操作。

描述 访问路径 方式
查询全部用户信息 http://localhost/users GET (查询)
查询指定用户信息 http://localhost/users/1 GET (查询)
添加用户信息 http://localhost/users POST(新增/保存)
修改用户信息 http://localhost/users PUT(修改/更新)
删除用户信息 http://localhost/users/1 DELETE (删除)

注:① 上述行为是约定方式,约定不是规范,可以打破,所以称 REST 风格,而不是 REST 规范。

② 描述模块的名称通常使用复数,也就是加 s 的格式描述,表示此类资源,而非单个资源,例如:users、books、accounts...

根据 REST 风格对资源进行访问称为 RESTFUL。


二、涉及注解

2.1 @RequestMapping

  • 类型 :方法注解

  • 位置:SpringMVC 控制器上方、控制器方法上方

  • 作用:设置当前控制器方法请求访问路径

  • 属性value (默认):请求访问路径
        method :http 请求动作,标准动作(GET/POST/PUT/DELETE)

  • 举例

    java 复制代码
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody User user){
        System.out.println("user save ... " + user);
        return "{'module':'user save'}";
    }

2.2 @PathVariable

  • 类型 :方形参注解

  • 位置:SpringMVC 控制器方法形参定义前面

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

  • 举例

    java 复制代码
    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id){
        System.out.println("user delete ... " + id);
        return "{'module':'user delete'}";
    }

2.3 @RestController

  • 类型 :类注解

  • 位置:SpringMVC 控制器定义上方

  • 作用 :设置当前控制器类为 RESTFUL 风格,等同于 @Controller 与 @ResponseBody 两个注解组合功能

  • 举例

    java 复制代码
    @RestController
    @RequestMapping("/users")
    public class UserController2 {
    	...
    }

2.4 @GetMapping、@PostMapping、@PutMapping、@DeleteMapping

  • 类型 :方法注解

  • 位置:SpringMVC 的 RESTFUL 开发的控制器方法定义上方

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

  • 举例 :GetMapping 对应 GET请求

    java 复制代码
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("user getById ... " + id);
        return "{'module':'user getById'}";
    }

注: @GetMapping("/{id}") 的作用相当于 @RequestMapping(value = "/{id}", method = RequestMethod.GET)(当注解里面要定义两个属性时,value 这个属性名要写出来),其余注解同理。


补充:@PathVariable、@RequestBody、@RequestParam 区别与应用

  • 区别 :① @RequestParam 用于接收 url 地址传参或表单传参。
       ② @RequestBody 用于接收 json 数据。
       ③ @PathVariable 用于接收路径参数,使用(参数名称)描述路径参数。
  • 应用 :① 当请求参数数量较少时,可以采用 @Pathvariable 接收请求路径变量,通常用于传递 id 值。
       ② 当请求参数超过 1 个时,以 json 格式为主,通常使用 @RequestBody。
       ③ 如果发送非 json 格式数据,选用 @RequestParam 接收请求参数。

三、REST风格案例

REST 风格案例1:

java 复制代码
@Controller
public class UserController {

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody // 返回 json 形式
    public String save(@RequestBody User user){
        System.out.println("user save ... " + user);
        return "{'module':'user save'}";
    }

    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id){
        System.out.println("user delete ... " + id);
        return "{'module':'user delete'}";
    }

    @RequestMapping(value = "/users", method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody User user){
        System.out.println("user update ... " + user);
        return "{'module':'user update'}";
    }

    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable Integer id){
        System.out.println("user getById ... " + id);
        return "{'module':'user getById'}";
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    @ResponseBody
    public String getAll(){
        System.out.println("user getAll");
        return "{'module':'user getAll'}";
    }
}

基于案例1的改造:

java 复制代码
@RestController
@RequestMapping("/users")
public class UserController2 {

    //@RequestMapping(value = "/users", method = RequestMethod.POST)
    @PostMapping
    public String save(@RequestBody User user){
        System.out.println("user save ... " + user);
        return "{'module':'user save'}";
    }

    //@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id){
        System.out.println("user delete ... " + id);
        return "{'module':'user delete'}";
    }

    //@RequestMapping(value = "/users", method = RequestMethod.PUT)
    @PutMapping
    public String update(@RequestBody User user){
        System.out.println("user update ... " + user);
        return "{'module':'user update'}";
    }

    //@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("user getById ... " + id);
        return "{'module':'user getById'}";
    }

    //@RequestMapping(value = "/users", method = RequestMethod.GET)
    @GetMapping
    public String getAll(){
        System.out.println("user getAll");
        return "{'module':'user getAll'}";
    }
}

参考链接:黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)

相关推荐
唐叔在学习几秒前
Python桌面端应用最小化托盘开发实践
后端·python·程序员
yuhaiqiang16 分钟前
被 AI 忽悠后,开始怀念搜索引擎了?
前端·后端·面试
sxlishaobin40 分钟前
Java I/O 模型详解:BIO、NIO、AIO
java·开发语言·nio
二闹1 小时前
Python文件读取三巨头你该选择哪一个?
后端·python
彭于晏Yan1 小时前
Spring AI(二):入门使用
java·spring boot·spring·ai
有一个好名字1 小时前
vibe codeing 开发流程
java
兑生1 小时前
【灵神题单·贪心】3745. 三元素表达式的最大值 | 排序贪心 | Java
java·开发语言
苏三说技术1 小时前
推荐几个牛逼的AI Agent项目
后端
polaris06301 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
卓怡学长1 小时前
m280本科生导师指导平台
java·数据库·spring·tomcat·maven·intellij-idea