REST风格介绍

 REST简介

1)传统风格描述形式

查询单个对象:http://localhost/user/getUserById?id=1

保存对新h:ttp://localhost/user/saveUser

2)REST风格描述形式

http://localhost/user/1

http://localhost/user

3)REST风格优点

3.1 隐藏资源的访问行为,无法通过地址得知对资源是何种操作。

3.2 书写简化

4)按照RESY风格访问资源时使用行为动作区分对资源进行了何种操作

http://localhost/users    查询全部用户信息     @RequestMapping(method = RequestMethod.GET)

http://localhost/users/1   查询指定用户心       GET(查询)

http://localhost/users      添加用户信息           POST(新增/保存)

http://localhost/users      修改用户信息           PUT(修改/更新)

http://localhost/users/1    删除用户信息          DELETE(删除)

注:此处的 users 代表模块名,描述的模块名通常使用复数,也就是加s的风格描述,表示此类资源而非单个资源;查询全部、添加径虽然是一样的,查询单个和删除请求路径是一样的,但是他们的请求方法不同

5)根据REST风格对资源进行访问称为RESTful

RESTful 入门案例

/**
 * @author 邓林妹
 * @description TODO
 * @date 2024/1/3
 */
@RestController
public class TestController {

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public String save(@RequestBody User user) {
        System.out.println("user save=" + user);
        return "{'module' : 'user save'}";
    }

    @RequestMapping(value = "/users", method = RequestMethod.PUT)
    public String update(@RequestBody User user) {
        System.out.println("user update=" + user);
        return "{'module' : 'user update'}";
    }

    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(value = "id") Integer id) {
        System.out.println("user delete=" + id);
        return "{'module' : 'user delete'}";
    }

    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    public String get(@PathVariable(value = "id") Integer id) {
        System.out.println("user get=" + id);
        return "{'module' : 'user get'}";
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    public String getAll() {
        System.out.println("user getAll");
        return "{'module' : 'user getAll'}";
    }
}

请求方式:

RESTful快速开发

/**
 * @author 邓林妹
 * @description TODO
 * @date 2024/1/3
 */
@RestController
@RequestMapping("/users") //提取公共的模块名
public class TestController {

    @PostMapping // 等价于 @RequestMapping(method = RequestMethod.POST)
    public String save(@RequestBody User user) {
        System.out.println("user save=" + user);
        return "{'module' : 'user save'}";
    }

    @PutMapping //等价于 @RequestMapping(method = RequestMethod.PUT)
    public String update(@RequestBody User user) {
        System.out.println("user update=" + user);
        return "{'module' : 'user update'}";
    }

    @DeleteMapping(value = "/users/{id}")
    public String delete(@PathVariable(value = "id") Integer id) {
        System.out.println("user delete=" + id);
        return "{'module' : 'user delete'}";
    }

    @GetMapping(value = "/users/{id}")
    public String get(@PathVariable(value = "id") Integer id) {
        System.out.println("user get=" + id);
        return "{'module' : 'user get'}";
    }

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

备注说明:此篇文章是学习b站视频,自己跟着视频学习后记录下来的笔记;b站视频学习地址:

[补]知识加油站-03-RESTful快速开发_哔哩哔哩_bilibili

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值