什么是RestFul风格?

什么是RestFul风格?

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

REST即Representational State Transfer的缩写,可译为"表现层状态转化”。REST最大的几个特点为:资源、统一接口、URI和无状态。

RestFul特点包括:

1、每一个URI代表1种资源;
2、客户端使用GET、POST、PUT、DELETE4个表示操作方式的动词对服务端资源进行操作:GET用来获取资源,POST用来新建资源(也可以用于更新资源),PUT用来更新资源,DELETE用来删除资源;
3、通过操作资源的表现形式来操作资源;
4、资源的表现形式是XML或者HTML;
5、客户端与服务端之间的交互在请求之间是无状态的,从客户端到服务端的每个请求都必须包含理解请求所必需的信息。

URL

在WWW上,每一信息资源都有统一的且在网上唯一的地址,该地址就叫URL(Uniform Resource Locator,统一资源定位符),它是WWW的统一资源定位标志,就是指网络地址。
URL由三部分组成:资源类型、存放资源的主机域名、资源文件名。也可认为由4部分组成:协议、主机、端口、路径

传统方式的资源访问

http://localhot:8080/item/select?id=1 查询 GET
http://localhot:8080/item/insert 新增 POST
http://localhot:8080/item/update 更新 POST
http://localhot:8080/item/delete?id=1 删除 DELETE

得出结论:传统方式请求地址都不同。

RestFul风格

下面有两种不同请求URL方式(请求路径上参数的不同)
@PathVariable:将URL请求路径参数映射到方法参数中
@GetMapping(path = “/add/{a}/{b}”) 等价于 @RequestMapping(path = “/add/{a}/{b}”,method = RequestMethod.GET)
不同的方式都用对应的注解:@GetMapping、@PostMapping、@PutMapping、@DeleteMapping相当于简写了代码

@Controller
public class testController {

//  @RequestMapping(path = "/add",method = RequestMethod.GET)
    @GetMapping(path = "/add")
    @ResponseBody
    public String add(int a, int b){
        int add = a+b;
        return "add1结果为:"+add;
    }

//  @RequestMapping(path = "/add/{a}/{b}",method = RequestMethod.GET)
    @GetMapping(path = "/add/{a}/{b}")
    @ResponseBody
    public String add2(@PathVariable int a, @PathVariable int b){
        int add = a+b;
        return "add2结果为:"+add;
    }

}

在这里插入图片描述在这里插入图片描述
简单来理解的话RestFul风格就是请求路径相同,但根据不同的参数、请求方式不同而执行不同的方法,产生的结果也不同。
如果是传统方式有两个相同路径会直接报错
再来强烈的感受一下。两种不同的请求方式:GET和POST

@Controller
public class testController {

//    @RequestMapping(path = "/add/{a}/{b}",method = RequestMethod.GET)
        @GetMapping(path = "/add/{a}/{b}")
        @ResponseBody
        public String add2(@PathVariable String a, @PathVariable String b){
            String add = a+b;
            return "GET结果为:"+add;
        }

//    @RequestMapping(path = "/add/{a}/{b}",method = RequestMethod.POST)
        @PostMapping(path = "/add/{a}/{b}")
        @ResponseBody
        public String add4(@PathVariable String a, @PathVariable String b){
            String add = a+b;
            return "POST方式结果为:"+add;
        }
}

创建一个表单模拟POST请求:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>RestFul</title>
</head>
<body>
        <form action="http://127.0.0.1:8080/add/阿/波" method="post" >
            <input type="submit">
        </form>
</body>
</html>

访问路径相同(URL)、参数相同,来看看产生的不同结果:
在这里插入图片描述
在这里插入图片描述
RestFul主要是保护了我们请求头的数据,如果是传统方式,两个相同的请求路径,请求方式不同也可以实现
传统方式:

//    @RequestMapping(path = "/add",method = RequestMethod.GET)
        @GetMapping(path = "/add")
        @ResponseBody
        public String add1(String a, String b){
            String add = a+b;
            return "GET结果为:"+add;
        }
        

//     @RequestMapping(path = "/add",method = RequestMethod.POST)
        @PostMapping(path = "/add")
        @ResponseBody
        public String add2(String a, String b){
            String add = a+b;
            return "POST结果为:"+add;
        }

创建一个表单模拟传统方式POST请求:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
        <form action="http://127.0.0.1:8080/add" method="post" >
            <input type="text" name="a">
            <input type="text" name="b">
            <input type="submit">
        </form>
</body>
</html>

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

猜你喜欢

转载自blog.csdn.net/weixin_45377770/article/details/106921736