• RestFul风格


    一、RestFul风格

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

    功能

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

    传统方式操作资源:通过不同的参数来实现不同的效果!方法单一,post 和get

    • http://127.0.0.1/item/queryltem.action?id=1 查询,GET
    • http://127.0.0.1/item/saveltem.action新增,POST
    • http://127.0.0.1/item/updateltem.action更新,POST
    • http://127.0.0.1/item/deleteltem.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

    二、测试

    2.1、原来的方式

    定义一个RestFulController类

    package com.massimo.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

    测试效果:
    在这里插入图片描述

    2.2、restful风格

    在Spring MVC中可以使用@PathVariable注解,让方法参数的值对应绑定到一个URI模板变量上。

    package com.massimo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class RestFulController {
    
        @RequestMapping("/add/{a}/{b}")
        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

    测试效果:
    在这里插入图片描述

    2.3、使用method属性指定请求类型

    用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型如GET, POST, HEAD, OPTIONS,PUT, PATCH, DELETE, TRACE等

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

    测试效果:
    在这里插入图片描述

    2.4、使用注解指定请求类型

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

    测试效果:
    在这里插入图片描述

    三、小结

    Spring MVC的@RequestMapping注解能够处理HTTP请求的方法,比如GET, PUT, POST,DELETE以及PATCH。

    所有的地址栏请求默认都会是HTTP GET类型的。

    方法级别的注解变体有如下几个:组合注解

    @GetMapping
    @PostMapping
    @PutMapping
    @DeleteMapping
    @patchMapping
    
    • 1
    • 2
    • 3
    • 4
    • 5

    @GetMapping是一个组合注解
    它所扮演的是@RequestMapping(method = RequestMethod.GET)的一个快捷方式。
    平时使用的会比较多!

    使用路径变量的好外:

    • 使路径变得更加简洁;
    • 获得参数更加方便,框架会自动进行类型转换。
    • 通过路径变量的类型可以约束访问参数,如果类型不一样,则访问不到对应的请求方法;
  • 相关阅读:
    Anntec ZKUXFT XT2 FGPA卡DPDK使用方法
    javascript二维数组(16)去除空格
    测试/开发程序员,30而立,你是否觉得迷茫?又当何去何从......
    计算机毕业设计ssm校园排球联赛管理系统y513u系统+程序+源码+lw+远程部署
    图论17-有向图的强联通分量-Kosaraju算法
    计算机视觉学习——表面检测
    Typescript:(一)基本使用
    利用鸿蒙系统硬件实现音乐播放功能之优秀
    自监督学习的新前沿:大型模型在自然语言处理中的应用
    记一次 .NET 某工控软件 内存泄露分析
  • 原文地址:https://blog.csdn.net/Massimo__JAVA/article/details/125534185