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

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

 


http://www.kler.cn/a/584089.html

相关文章:

  • c++之STL库
  • 蓝桥杯嵌入式组第十二届省赛题目解析+STM32G431RBT6实现源码
  • 笔记:代码随想录算法训练营day43:LeetCode300.最长递增子序列、674. 最长连续递增序、 718. 最长重复子数组
  • 大模型在老年性白内障诊疗全流程中的应用研究报告
  • Android wgs84坐标系转CGCS2000坐标系
  • CentOS8+Zabbix7.2.4解决中文显示问题
  • rtsp在网页上显示(webrtc-stream)
  • Pytest自动化测试框架pytest-xdist分布式测试插件
  • JAVA面试_进阶部分_Java JVM:垃圾回收(GC 在什么时候,对什么东西,做了什么事情)
  • 【Unity】在项目中使用VisualScripting
  • 计算机视觉算法实战——驾驶员分心检测(主页有源码)
  • 大模型开源的工具包有哪些特殊符号可以使用;SEP 是什么
  • HTML 表格的详细介绍与应用
  • [洛谷]P1123 取数游戏
  • rv1106 PWM控制
  • javaWeb的详细笔记(超详细版本)
  • AI大数据挖掘的威力
  • 【鸿蒙开发】Hi3861学习笔记- GPIO之按键
  • 小白学习:提示工程(什么是prompt)
  • PostgreSQL存储管理体系结构学习笔记2