当前位置:网站首页>什么是Restful风格?与传统的访问风格有什么不同?
什么是Restful风格?与传统的访问风格有什么不同?
2022-07-13 19:03:00 【李奕赫揍小邰】
作者主页:李奕赫揍小邰的博客
* 个人介绍:大家好,我是李奕赫!( ̄▽ ̄)~*
记得点赞、收藏、评论️️️
认真学习!!!
什么是Restful风格?
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
REST即Representational State Transfer的缩写,可译为"表现层状态转化”。REST最大的几个特点为:资源、统一接口、URI和无状态。
重点!!!它仅仅是一种风格!!
Restful风格的特点
Restful风格中,资源操作都是统一接口的:
GET(SELECT):从服务器取出资源。
POST(CREATE):在服务器新建一个资源。
PUT(UPDATE):在服务器更新资源(客户端提供完整资源数据)。
PATCH(UPDATE):在服务器更新资源(客户端提供需要修改的资源数据)。
DELETE(DELETE):从服务器删除资源。
Restful风格与传统资源访问的区别
传统的资源访问
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风格访问
不同的方式都用对应的注解:@GetMapping、@PostMapping、@PutMapping、@DeleteMapping相当于简写了代码
@Controller
public class RESTfulController {
//传统方式:http://localhost:8080/add?a=1&b=1
@RequestMapping("add")
public String add(int a, int b , Model model){
int add1=a+b;
model.add("msg", "结果为:"+add1);
return "hello";
}
//RESTful:http://localhost:8080/add/1/1
//@RequestMapping(path = "/add/{a}/{b}",method = RequestMethod.GET)
@GetMapping(path = "/add/{a}/{b}")
@ResponseBody
public String add(@PathVariable int a, @PathVariable int b){
int add1= a+b;
return "add1结果为:"+add1;
}
访问的地址为:http://localhot:8080/add/1/1
使用@PathVariable将URL请求路径参数映射到方法参数中。
RestFul风格就是请求路径相同,但根据不同的参数、请求方式不同而执行不同的方法,产生的结果也不同。
总结:
Restful风格优点:
1、使请求路径变得更加简洁
2、传递、获取参数值更加方便,框架会自动进行类型转换
3、通过路径变量@PathVariable的类型,可以约束访问参数。
4、若参数值与定义类型不匹配,则访问不到对应的方法,报错400错误的请求。
安全,请求路径中直接传递参数值,并用斜线/分隔,不会暴露传递给方法的参数变量名。
5、高效,更易于缓存的实现,让响应更加高效。
边栏推荐
- Preorder and inorder traversal sequences determine a binary tree (restore binary tree)
- 【服务器数据恢复】某品牌MSA SAN存储RAID5瘫痪,上层LUN无法使用的数据恢复案例
- Precautions for Jerry's serial port to receive data by DMA [chapter]
- HCIP第五天实验
- 模块
- JMeter 21天打卡 day01
- LeetCode(剑指 Offer)- 45. 把数组排成最小的数
- [play with FPGA learning 7 in simple terms ----- cross clock domain signal processing based on FPGA]
- 有没有完全自主的国产化数据库技术
- babylon.js高度图
猜你喜欢

Either retire, change careers, or change management. PS hasn't blogged for two months

Flutter RenderFlex overflowed by pixels on the bottom键盘弹出警告异常

【无标题】

STM32 realizes nRF24L01 communication

HCIP第四天实验

3dmax2021 中的各种显示相关如何设置?

Record a detailed penetration test of a site

【服务器数据恢复】某品牌MSA SAN存储RAID5瘫痪,上层LUN无法使用的数据恢复案例
![[play with fpga8 in simple terms ----- metastable]](/img/93/8d85568d6cc264b5d0b7d91150cd76.png)
[play with fpga8 in simple terms ----- metastable]

vscode 输入 !不提示,没法自动补全的解决方法(最新)
随机推荐
How to solve the problem that the computer shared file cannot be opened
Hcip second day notes
C语言开发环境搭建:VSCode+GCC
红柳林,煤矿里挖出了数据金矿!
A brief introduction to shell syntax
Left leaning heap - Analysis and Implementation
先序和中序遍曆序列確定一顆二叉樹(還原二叉樹)
浅谈——对技术转型做管理的看法
Preorder and inorder traversal sequences determine a binary tree (restore binary tree)
首届人工智能安全大赛启动报名 三大赛题等你来战
Hcip second experiment
This article takes you to understand what tensors are in deep learning, how their operations are, how to understand tensors, and the dimensions of tensors, which are easy to understand
Quick news: Youxian responded to the closure of 9 cities in 3 days every day; Mingchuangyou products will be listed on the Hong Kong Stock Exchange
Detailed explanation and use of interface mock
有没有完全自主的国产化数据库技术
快讯:京东科技发布“百亿收入计划”;博通软件业务总裁离职
Flutter RenderFlex overflowed by pixels on the bottom键盘弹出警告异常
Could not connect to redis at 192.168.164.118:6379: connection rejected under Linux
Otacle table query
Jerry's key switches the default EQ and EQ adjusted by EQ tool [chapter]