当前位置:网站首页>什么是Restful风格以及它的四种具体实现形式
什么是Restful风格以及它的四种具体实现形式
2022-07-26 13:59:00 【努力努力再努力Gzc.】
一、背景
在之前我们进行项目开发时,我们一般不注重区分前端向后端请求数据的方式,不管你前端传过来的是get请求还是post请求,即不管是哪一种请求,我们都可以用@RequestMapping注解来处理。
但是如果在进行项目开发时,组里老大规定我们必须明确处理我们前端到底是哪一种请求,是get还是post?是put还是delete?那这时候其实我们就要以restful风格来处理我们的前端请求了。同时,restful也能实现:在请求路径相同(比如几个请求路径均为"/user/test")的情况下,我们可以依据请求方法的不同,来匹配不同的后端接口。
二、Restful的四种请求实现
restful主要有以下四种实现,也就是对应我们的四种http请求方式:
(1)GET: 获取数据
(2)POST: 添加数据
(3)PUT: 修改数据
(4)DELETE: 删除数据
在项目中,GET,POST,PUT,DELETE四种请求方式分别对应@GetMapping、PostMapping、@PutMapping、@DeleteMapping。每个注解各司其职,只能干自己分内的事情,即@GetMapping只能处理get请求,不能处理其他三种请求。
1、GET即数据库的select操作: 请求会向数据库发查询数据的请求,从而来获取信息,其只是用来查询一下数据,不会修改、增加或更新数据,不会影响资源的内容。无论进行多少次操作,数据库里的内容都不会改变的。
2、PUT即数据库的update操作:请求是向服务器端发送数据的,从而改变信息,其用来修改数据的内容,但是不会增加数据等,无论进行多少次PUT操作,数据库都不会新增或减少数据。
3、POST即数据库的insert操作:请求同PUT请求类似,都是向服务器端发送数据的,但是该请求会新增数据,几乎目前所有的提交操作都是用POST请求的(比如用户注册)。
4、DELETE即数据库的delete操作:请求是用来删除某一个资源的。
POST主要作用在一个集合资源之上的(url),而PUT主要作用在一个具体资源之上的(url/xxx).如URL可以在客户端确定,那么可使用PUT,否则用POST。
一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE四种请求方式就对应着对数据库中这个资源进行查,增,改,删4个操作。
三、代码实例
@RestController
@RequestMapping("/user/test")
public class JoblevelController {
@Autowired
private JoblevelService joblevelService;
@ApiOperation(value = "获取所有职称")
@GetMapping("/")
public List<Joblevel> getAllJobLevels(){
return joblevelService.list();
}
@ApiOperation(value = "添加职称")
@PostMapping("/")
public RespBean addJobLevel(@RequestBody Joblevel joblevel){
if(joblevelService.save(joblevel)){
return RespBean.success("添加职称成功");
}
return RespBean.error("添加职称失败");
}
@ApiOperation(value = "修改职称")
@PutMapping("/")
public RespBean updateJobLevel(@RequestBody Joblevel joblevel){
if(joblevelService.updateById(joblevel)){
return RespBean.success("修改职称成功");
}
return RespBean.error("修改职称失败");
}
@ApiOperation(value = "删除职称")
@DeleteMapping("/{id}")
public RespBean deleteJobLevelById(@PathVariable Integer id){
if(joblevelService.removeById(id)){
return RespBean.success("删除职称成功");
}
return RespBean.error("删除职称失败");
}
@ApiOperation(value = "批量删除职称")
@DeleteMapping("/")
public RespBean deleteJobLevelByIds(Integer[] ids){
if(joblevelService.removeByIds(Arrays.asList(ids))){
return RespBean.success("批量删除职称成功");
}
return RespBean.error("批量删除职称失败");
}
}
边栏推荐
- 数据泄漏、删除事件频发,企业应如何构建安全防线?
- Explain four interesting NPM usages with charts
- Detailed explanation of alter field of MySQL Foundation
- Canvas upload image Base64 with cropping function jcrop.js
- .net6 encounter with the League of heroes - create a game assistant according to the official LCU API
- 「中高级试题」:MVCC实现原理是什么?
- C语言贪吃蛇-链表和指针练习
- Official announcement! Edweisen group and Baidu xirang reached a deep co creation cooperation
- Meeting seating and submission for approval of OA project
- Use the requests library to crawl web pages
猜你喜欢

Ros2 learning (1) introduction to ros2

估值15亿美元的独角兽被爆裁员,又一赛道遇冷?
![[paper reading] raw+:a two view graph propagation method with word coupling for readability assessment](/img/14/1a02d564c59724d04fce340b6b227d.png)
[paper reading] raw+:a two view graph propagation method with word coupling for readability assessment

基于多特征的技术融合关系预测及其价值评估

Docker integrates the redis sentinel mode (one master, two slave and three sentinels)

MySQL's practice of SQL analysis and optimization from the index principle

gdb常用命令

ROS2学习(1)ROS2简述

上一次听到易趣,还是上一次

Redis learning notes
随机推荐
消息的订阅和发布
Subscription and publication of messages
[dark horse morning post] many apps under bytek have been taken off the shelves; The leakage of deoxidizer in three squirrels caused pregnant women to eat by mistake; CBA claimed 406million yuan from
Integer internal cache
Convert the array in JSON file to struct
Familiarize you with the "phone book" of cloud network: DNS
The shell script in Jenkins fails to execute but does not exit by itself
万字长文,浅谈企业数字化建模蓝图
Native JS get transform value x y z and rotate rotation angle
ROS2学习(1)ROS2简述
How to remove black edges from hyperimage images (two methods)
Cavans realizes Static Rolling barrage
Tianyi cloud web application firewall (edge cloud version) supports the detection and interception of Apache spark shell command injection vulnerabilities
PHP uses sqlserver
421. Maximum XOR value of two numbers in the array
「中高级试题」:MVCC实现原理是什么?
The picture moves horizontally with the phone - gyroscope. 360 degree setting conditions
上一次听到易趣,还是上一次
Explain four interesting NPM usages with charts
Videojs to canvas pause, play, switch video