SpringMVC——REST简介及入门案例

ops/2025/3/18 22:45:31/

REST简介

    REST(Representational State Transfer)即表现层状态转移,是一种基于HTTP协议的网络应用程序的架构风格。它强调客户端和服务器之间的交互操作,通过对资源的表现形式进行操作来实现对资源的管理。REST风格的API设计具有简单、灵活、可扩展等特点,因此在Web开发中得到了广泛应用。

优点

  • 隐藏资源的访问路径,无法通过地址资源得知对资源是何种操作
  • 书写简化

REST风格

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

  • http://localhost/users                       查询全部用户信息  GET(查询)
  • http://localhost/users/1                    查询指定用户信息  GET(查询)
  • http://localhost/users                       添加用户信息  POST(新增/保存)
  • http://localhost/users                       修改用户信息  PUT(修改/更新)
  • http://localhost/users/1                    删除用户信息  DELETE(删除) 

 注: 上述行为是约定方式,并不是规范,描述模块的名称通常是复数。

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

RESTful入门案例

    截止目前的学习,当前写法为:

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

REST写法为:

java">@Controller
public class UserController {@RequestMapping(value = "/users",method = RequestMethod.POST)@ResponseBodypublic 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'}";}
}

其中:@PathVariable表示路径变量作用是绑定路径参数与处理器方法形参间的关系(路径参数名要与形参名一致),传入参数时无需向之前?key=value那样,@RequestMapping中value的值也要接上{参数名}用于传递参数

简化开发

    在上面的案例中,有许多重复使用的注解

  •     @RequestMapping将重复的路径提取到类上面,同样@ResponseBody同理。
  •     @ResponseBody可以和@Controller合并成@RestController。
  •     @RequestMapping(method = RequestMethod.POST)可以简化成@PostMapping
  •     @RequestMapping内还有value时也可以进行简化@DeleteMapping("/{id}")

最终的写法如下:

java">@RestController
@RequestMapping("/users")
public class UserController {@PostMappingpublic String save(@RequestBody User user) {System.out.println("user save..." + user);return "{'module':'user save'}";}@DeleteMapping("/{id}")public String delete(@PathVariable Integer id) {System.out.println("user delete..." + id);return "{'module':'user delete'}";}@PutMappingpublic String update(@RequestBody User user) {System.out.println("user update..." + user);return "{'module':'user update'}";}@GetMapping("/{id}")public String getById(@PathVariable Integer id) {System.out.println("user getById..." + id);return "{'module':'user getById'}";}@GetMappingpublic String getAll() {System.out.println("user getAll" );return "{'module':'user getAll'}";}
}

 


http://www.ppmy.cn/ops/166886.html

相关文章

数学建模:常用模型

数学建模四大模型总结 数学建模常见模型整理(简单介绍) 建模流程: 首先,对题目分析,判断题目是属于哪一类建模问题。 再从对应分类的建模方法里面进行选择。(查找文献,随机应变)…

HTML 新手入门:从零基础到搭建第一个静态页面(二)

构建第一个静态页面 (一)规划页面结构 在开始编写代码之前,我们需要先规划好页面的结构。这就好比在建造房屋之前,需要先设计好房屋的布局一样。思考一下你希望页面展示哪些内容,比如是一篇文章、一组图片&#xff0…

如何在Django中有效地使用Celery进行定时任务?

当我们谈到Web开发时,Django无疑是一个非常流行的框架。而Celery则是与Django配合使用的强大任务队列工具。今天,我们来聊聊如何在Django中使用Celery来实现定时任务。定时任务在很多场景下都非常有用,比如定期发送邮件、清理数据库、执行数据…

深入解析网络相关概念​​

网络的发展及体系结构​ 网络的发展经历了从简单的计算机连接到如今全球化复杂网络的过程。早期以 ARPANET 为代表,奠定了分组交换网络的基础。随着时间推移,网络规模不断扩大,各种网络技术层出不穷。​ 网络体系结构采用分层模型&#xff…

【紫光同创FPGA开发常用工具】FPGACPLD的下载与固化

文档内容适配技术问题说明(非正文): 1、FPGA&CPLD如何下载位流文件; 2、FPGA外部flash如何固化位流文件; 3、PDS软件烧录界面如何新增用户flash; 4、CPLD内部flash如何固化位流文件; F…

【C语言】函数和数组实践与应用:开发简单的扫雷游戏

【C语言】函数和数组实践与应用:开发简单的扫雷游戏 1.扫雷游戏分析和设计1.1扫雷游戏的功能说明(游戏规则)1.2游戏的分析与设计1.2.1游戏的分析1.2.2 文件结构设计 2. 代码实现2.1 game.h文件2.2 game.c文件2.3 test.c文件 3. 游戏运行效果4…

angular九宫格ui

说明:angular九宫格ui 效果图: step1: C:\Users\wangrusheng\WebstormProjects\untitled4\src\app\order\order.component.ts import { Component } from angular/core; import {NgForOf} from angular/common;interface Order {title: string;price:…

Unity 从零开始的框架搭建1-6 读Excel配置表自动生成json和数据结构类

本人水平有限 如有不足还请斧正,本文仅作学习交流使用不做任何商业用途 目录 效果 Excel结构 需要插件 读表算法 代码 效果 Unity Excel一键读表转json和数据结构类 Excel结构 需要插件 Epplus的dll即可 可以网上搜索 或者用Vs工具下载 另外我已上传资源 读表算法 看…