一文解读如何应用 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完整版)

相关推荐
重生之我要进大厂12 分钟前
LeetCode 876
java·开发语言·数据结构·算法·leetcode
_祝你今天愉快15 分钟前
技术成神之路:设计模式(十四)享元模式
java·设计模式
计算机学姐1 小时前
基于python+django+vue的影视推荐系统
开发语言·vue.js·后端·python·mysql·django·intellij-idea
小筱在线1 小时前
SpringCloud微服务实现服务熔断的实践指南
java·spring cloud·微服务
JustinNeil1 小时前
简化Java对象转换:高效实现大对象的Entity、VO、DTO互转与代码优化
后端
luoluoal1 小时前
java项目之基于Spring Boot智能无人仓库管理源码(springboot+vue)
java·vue.js·spring boot
ChinaRainbowSea1 小时前
十三,Spring Boot 中注入 Servlet,Filter,Listener
java·spring boot·spring·servlet·web
小游鱼KF1 小时前
Spring学习前置知识
java·学习·spring
扎克begod1 小时前
JAVA并发编程系列(9)CyclicBarrier循环屏障原理分析
java·开发语言·python
青灯文案11 小时前
SpringBoot 项目统一 API 响应结果封装示例
java·spring boot·后端