• SpringMVC 04 RestFul风格


    4.1 RestFul风格


    概念: Restful 就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

    功能:

    • 资源:互联网所有的事物都可以被抽象为资源
    • 资源操作:使用 POSTDELETEPUTGET,使用不同方法对资源进行操作。
    • 分别对应 添加删除修改查询

    传统方式操作资源: 通过不同的参数来实现不同的效果,这样的话 方法单一,就只要 原生态的 POSTGET

    • http://127.0.0.1/item/queryItem.action?id=1 查询操作 GET
    • http://127.0.0.1/item/saveItem.action 新增 POST
    • http://127.0.0.1/item/updateItem.action 更新 POST
    • http://127.0.0.1/item/deleteItem.action?id=1 删除 GET 或 POST

    使用 RestFul 操作资源: 可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!

    • http://127.0.0.1/item/1 查询 GET
    • http://127.0.0.1/item 新增 POST
    • http://127.0.0.1/item 更新 PUT
    • http://127.0.0.1/item/1 删除 DELETE

    4.2 RestFul风格 的使用

    @RequestMapping(path="请求路径/{参数1}/{参数2}",method=RequestMethod.GET):以 GET 方式 去 发送 这个请求。

    path的话可以 写为 value,但是 不方便理解和记忆。所以最好是写path。

    当然还有如下四个衍生 注解:使用起来 会 更加的方便。

    @GetMapping("请求路径/{参数1}/{参数2}")
    @PostMapping("请求路径/{参数1}/{参数2}")
    @PutMapping("请求路径/{参数1}/{参数2}")
    @DeleteMapping("请求路径/{参数1}/{参数2}")

    这个地方 说的 参数1 和 参数2 指的就是 当前 这个请求方法 的 形参。只不过 你必须 用 @PathVariable 去修饰它们。

    package top.muquanyu.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PatchMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    
    @Controller
    public class RestFulController {
    
        @RequestMapping(path="/add/{a}/{b}",method = RequestMethod.GET)
        public String test1(@PathVariable int a, @PathVariable int b, Model model){
            int res = a + b;
            model.addAttribute("msg","结果为:" + res) ;
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

        @GetMapping("/add/{a}/{b}")
        public String test2(@PathVariable int a, @PathVariable int b, Model model){
            int res = a + b;
            model.addAttribute("msg","结果为:" + res) ;
            return "hello";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    优点:

    • 拼接和书写简单
    • 效率高(支持缓存)
    • 安全

    4.3 传统风格的使用

    请求方法的形参 也是 我们 发送请求的时候,路径里面 的 参数。它们之间是一一对应的。所以我们 设置完参数后,直接就可以 使用传统风格。

    package top.muquanyu.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    
    @Controller
    public class RestFulController {
    
        @RequestMapping("/add")
        public String test1(int a, int b, Model model){
            int res = a + b;
            model.addAttribute("msg","结果为:" + res) ;
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    如果你要是 直接 请求 add 这个路径,肯定会报错,因为我们 已经在这个请求方法里 设定 a 和 b 这两个参数了。所以你在 请求的时候必须携带这两个参数。

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    ChatGPT 设计游戏剧情 | 基于 AI 5 天创建一个农场游戏,完结篇!
    django-admin登录窗口添加验证码功能-(替换原有的login.html)captcha插件
    [MATLAB]:基础知识学习
    mybatisplus表字段是中文时启动项目报错(字符串访问越界)
    【HTML】播放器如何自动播放【已解决】
    MySQL定时整库备份&滚动删除指定日期前的备份数据
    轻松虚拟gps定位 AnyGo中文 for mac
    scipy库统计模块stats
    Zookeeper实战案例(1)
    排序:冒泡排序算法分析
  • 原文地址:https://blog.csdn.net/qq_52606908/article/details/125896861