当前位置:网站首页>什么是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("批量删除职称失败");
}
}
边栏推荐
- [oauth2] v. oauth2loginauthenticationfilter
- Multi objective optimization series 1 --- explanation of non dominated sorting function of NSGA2
- Docker swarm cluster builds highly available MySQL active and standby
- [oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer
- Add a display horizontal line between idea methods
- Integer internal cache
- 12437 words, take you to explore the principle of RPC communication
- 基于机器学习的技术术语识别研究综述
- Detailed explanation of alter field of MySQL Foundation
- 周伟:寻找非共识性投资机会,陪伴延迟满足的创始团队
猜你喜欢

融合多自然语言处理任务的中医辅助诊疗方案研究——以糖尿病为例

基于双层主题模型的技术演化分析框架及其应用
![[NOIP2003 普及组]栈](/img/95/871b1c6f492b467bffd25912304b44.gif)
[NOIP2003 普及组]栈

Comparison between SIGMOD function and softmax function

Frisbee, 2022 "black red" top stream

WPS凭什么拒绝广告?

基于专利多属性融合的技术主题划分方法研究

Inspiration from brain: introduction to synaptic integration principle in deep neural network optimization

JS download files, filesaver.js export txt and Excel files

Polymorphic case - making drinks
随机推荐
MySQL's practice of SQL analysis and optimization from the index principle
Code cloud, which officially supports the pages function, can deploy static pages
JS get the current time, time and timestamp conversion
Ros2 learning (1) introduction to ros2
[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
Meeting seating and submission for approval of OA project
GDB common commands
Plato farm is expected to further expand its ecosystem through elephant swap
Integer internal cache
技术分享 | 需要小心配置的 gtid_mode
Construction practice of pipeline engine of engineering efficiency ci/cd
Red * is added to the input box to indicate mandatory items
Familiarize you with the "phone book" of cloud network: DNS
Technology sharing | gtid that needs to be configured carefully_ mode
Synchronization mechanism of go (sync.mutex)
一文学透MySQL表的创建和约束
Book download | introduction to lifelong supervised learning in 2022, CO authored by meta AI, CMU and other scholars, 171 Pages pdf
Share 44 JS problems, and half of them are masters
[oauth2] v. oauth2loginauthenticationfilter
The difference between V-model and.Sync modifier