当前位置: 首页 > news >正文

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

文章目录

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


一、REST 简介

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

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

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

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

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

注:① 上述行为是约定方式,约定不是规范,可以打破,所以称 REST 风格,而不是 REST 规范。
     ② 描述模块的名称通常使用复数,也就是加 s 的格式描述,表示此类资源,而非单个资源,例如:users、books、accounts…

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


二、涉及注解

2.1 @RequestMapping

  • 类型方法注解
  • 位置:SpringMVC 控制器上方、控制器方法上方
  • 作用:设置当前控制器方法请求访问路径
  • 属性value (默认):请求访问路径
        method :http 请求动作,标准动作(GET/POST/PUT/DELETE)
  • 举例
    @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 控制器方法形参定义前面
  • 作用:绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应
  • 举例
    @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 两个注解组合功能
  • 举例
    @RestController
    @RequestMapping("/users")
    public class UserController2 {...
    }
    

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

  • 类型方法注解
  • 位置:SpringMVC 的 RESTFUL 开发的控制器方法定义上方
  • 作用:设置当前控制器方法 请求访问路径与请求动作,每种对应一个请求动作。
  • 举例:GetMapping 对应 GET请求
    @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:

@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)@ResponseBodypublic String delete(@PathVariable Integer id){System.out.println("user delete ... " + id);return "{'module':'user delete'}";}@RequestMapping(value = "/users", method = RequestMethod.PUT)@ResponseBodypublic String update(@RequestBody User user){System.out.println("user update ... " + user);return "{'module':'user update'}";}@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)@ResponseBodypublic String getById(@PathVariable Integer id){System.out.println("user getById ... " + id);return "{'module':'user getById'}";}@RequestMapping(value = "/users", method = RequestMethod.GET)@ResponseBodypublic String getAll(){System.out.println("user getAll");return "{'module':'user getAll'}";}
}

基于案例1的改造:

@RestController
@RequestMapping("/users")
public class UserController2 {//@RequestMapping(value = "/users", method = RequestMethod.POST)@PostMappingpublic 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)@PutMappingpublic 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)@GetMappingpublic String getAll(){System.out.println("user getAll");return "{'module':'user getAll'}";}
}

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

http://www.lryc.cn/news/193694.html

相关文章:

  • 使用JAVA发送邮件
  • 【JavaEE】_servlet程序的编写方法
  • 美国市场三星手机超苹果 中国第一属华为
  • nodejs+vue+elementui医院挂号预约管理系统4n9w0
  • 调试技巧(课件图解)
  • react中获取input输入框内容的两种方法
  • Linux基础—1
  • 十个面试排序算法
  • 技术学习群-第四期内容共享
  • 冒泡排序/鸡尾酒排序
  • 代码随想录算法训练营第五十三天|309.最佳买卖股票时机含冷冻期、714.买卖股票的最佳时机含手续费
  • 【Docker】Docker的使用案例以及未来发展、Docker Hub 服务、环境安全、容器部署安全
  • qt qtabwidget获取当前选项卡的所有按键
  • 为什么Excel插入图片不显示,点击能够显示
  • 使用Python创建faker实例生成csv大数据测试文件并导入Hive数仓
  • qml基础语法
  • 数据结构 - 2(顺序表10000字详解)
  • 05在IDEA中配置Maven的基本信息
  • 基于IDEA 配置Maven环境和JDK版本(全局)
  • mysql数据库 windows迁移至linux
  • P4491 [HAOI2018] 染色
  • 12096 - The SetStack Computer (UVA)
  • Pygame中将鼠标形状设置为图片2-1
  • Vue3 + Nodejs 实战 ,文件上传项目--实现图片上传
  • linux C++ vscode连接mysql
  • [资源推荐]langchain、LLM相关
  • hdfs笔记
  • java_方法引用和构造器引用
  • Flink Log4j 2.x使用Filter过滤日志类型
  • Ubuntu下怎么配置vsftpd