• REST风格


    1. REST简介

    • REST,表现形式状态转换,是一种软件架构风格

      • 传统风格访问资源描述形式
        • http://localhost/user/getById?id=1 查询id为1的用户信息
        • http://localhost/user/save 添加用户信息
      • REST风格描述形式
        • http://localhost/users/1 查询id为1的用户信息
        • http://localhost/users 添加用户信息
    • 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

    2. RESTful的使用

    1. 方式一
    @Controller
    public class UserController {
    
        @RequestMapping(value = "/users",method = RequestMethod.POST)
        @ResponseBody
        public String save(@RequestBody User user){
            System.out.println("user save user ===>"+user);
            return "{'msg':'user save'}";
        }
    
        @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
        @ResponseBody
        public String delete(@PathVariable Integer id){
            System.out.println("user delete id ===>"+id);
            return "{'msg':'user delete'}";
        }
        
        @RequestMapping(value = "/users",method = RequestMethod.GET)
        @ResponseBody
        public String getAll(){
            System.out.println("user getAll  ===>");
            return "{'msg':'user getAll'}";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 方式二
    @RestController  //等同于@Controller和@ResponseBody
    @RequestMapping("/books")
    public class BookController {
    
        @PostMapping
        public String save(@RequestBody Book book){
            System.out.println("user save book ===>"+book);
            return "{'msg':'book save'}";
        }
    
        @DeleteMapping("/{id}")
        public String delete(@PathVariable Integer id){
            System.out.println("book delete id ===>"+id);
            return "{'msg':'book delete'}";
        }
        
        @GetMapping
        public String getAll(){
            System.out.println("book getAll  ===>");
            return "{'msg':'book getAll'}";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.@RequestParam、@RequestBody和@PathVariable的区别和应用

    1. 区别
    • @RequestParam 用于接收url地址传参或者表单传参
    • @RequestBody 用于接收json数据
    • @PathVariable 用于接收路径参数,使用(参数名称)描述路径参数
    1. 应用
    • @RequestParam 发送非json格式数据,选用@RequestParam接收请求参数
    • RequestBody 发送请求参数超过一个时,以json格式为主,RequestBody 应用较广
    • @PathVariable 采用RESTful开发,当参数数量较少时,可以采用@PathVariable接收请求路径变量
  • 相关阅读:
    易语言实现植物大战僵尸新手cheat体验
    学习笔记:SpringCloud 微服务技术栈_高级篇②_分布式事务
    基于yolov5的车辆行人道路检测
    透明安全地解释Moonbeam基金会分配的GLMR去了哪
    给STM32装点中国风——华为LiteOS移植
    数据治理之考评环节
    3.Bean的作用域与生命周期
    有目的的脉动调查:有效的员工参与度调查的 5 个考虑因素
    导入fetch_california_housing 加州房价数据集报错解决(HTTPError: HTTP Error 403: Forbidden)
    Nebula Studio:部署与连接
  • 原文地址:https://blog.csdn.net/m0_59564754/article/details/127872312