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

SpringMVC——REST简介及入门案例

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入门案例

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

@Controller
public 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("/getAll")
    @ResponseBody
    public String getAll() {
        System.out.println("user getAll" );
        return "{'module':'user getAll'}";
    }
}

REST写法为:

@Controller
public class UserController {

    @RequestMapping(value = "/users",method = RequestMethod.POST)
    @ResponseBody
    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'}";
    }
}

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

简化开发

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

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

最终的写法如下:

@RestController
@RequestMapping("/users")
public class UserController {

    @PostMapping
    public 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'}";
    }

    @PutMapping
    public 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'}";
    }

    @GetMapping
    public String getAll() {
        System.out.println("user getAll" );
        return "{'module':'user getAll'}";
    }
}

 

相关文章:

  • 前置机跟服务器的关系
  • 文件操作2
  • node.js-WebScoket心跳机制(服务器定时发送数据,检测连接状态,重连)
  • C语言刷题第三章(下)
  • WPF未来展望:紧跟技术发展趋势,探索新的可能性
  • 《基于大数据的营养果蔬推荐系统的设计与实现》开题报告
  • C++ STL—— String库
  • Vue3中 ref 与 reactive区别
  • OKHttp3 源码阅读 - Kotlin版本
  • 基于WebRTC技术的EasyRTC嵌入式音视频SDK:多平台兼容与性能优化
  • 以实现生产制造、科技研发、人居生活等一种或多种复合功能的智慧油站开源了
  • GraphRAG 融合 RAG:双剑合璧,精度更上一层楼
  • [超详细]JAVA接入DeepSeek保姆级教学[小白]
  • 图论part3|101.孤岛的总面积、沉没孤岛、417. 太平洋大西洋水流问题
  • 考研408-数据结构完整代码 线性表的顺序存储结构 - 顺序表
  • Unity2D 井字棋
  • 双路快排--力扣215.数组中的第K个最大元素(java)
  • 车载以太网测试-9【网络层】-子网划分的子网掩码VLAN
  • 项目组织管理类型-职能式组织和矩阵式组织的区别
  • 开发策略选择:如何为项目找到最优路径?
  • 特朗普与泽连斯基通话
  • 第十届青春文学奖揭晓,梁晓声获特别奖
  • 因救心梗同学缺席职教高考的姜昭鹏顺利完成补考
  • 林诗栋/蒯曼混双取胜,国乒赢得多哈世乒赛开门红
  • 七猫征文大赛颁出112万奖金,非遗题材作品斩获金奖
  • 丹麦外交大臣拉斯穆森将访华