当前位置:网站首页>RESTful-URL设计规范
RESTful-URL设计规范
2022-07-21 19:48:00 【暇光署墨】
前后端分离的项目中,前后端之间是接口进行请求和响应,后端向前端提供请求时就要对外暴露一个URL;URL的设计不能是随意的,需要准从一定的设计规范---RESTful
目录
一、RESTful简介 :
RESTful是一种web api的标准,也就是一种url设计规范。
二、 RESTful规范:
RESTful规范需要遵守三个规则,分别如下:
- 每个URL请求路径代表服务器上唯一的资源
- 使用不同的请求方式表示不同的操作
- 接口响应的资源表现形式采用json
(1)、 每个URL请求路径代表服务器上唯一的资源;如果进行删除,在传统的url设计中,使用到了相同的url路径(http://localhost:8080/goods/delete),所以不满足RESTful规范的第一个需要遵守的准则,而在RESTful设计中则每个URL请求路径代表服务器上唯一的资源(路径唯一)
传统的url设计:
http://localhost:8080/goods/delete?goodId=1 商品1
http://localhost:8080/goods/delete?goodId=2 商品2
RESTful设计:
http://localhost:8080/goods/delete/1 商品1
http://localhost:8080/goods/delete/2 商品2
@ApiOperation(value = "商品删除功能")
@RequestMapping(value = "/delete/{gid}",method = RequestMethod.DELETE)
public ResultVo addGoods(@PathVariable("gid") int gid) {
System.out.println(gid);
return null;
}
打开接口文档:http://localhost:8080/doc.html进行调试
控制台查看结果:输出“1”,则请求成功~
(2)、使用不同的请求方式表示不同的操作。springmvc对RESTful风格提供了很好的支持,在我们定义一个接口的url时,可以通过 @RequestMapping(value = "/delete/{gid}",method = RequestMethod.DELETE)指定请求方式。也可以使用特定的请求方式注解进行测定
post | 添加 |
get | 查询 |
put | 修改 |
delete | 删除 |
option | 预检 |
package com.xgsm.Fmmall.controller;
import com.xgsm.Fmmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@Api(value = "提供商品添加、修改、删除及查询的相关接口", tags = "商品管理")
@RequestMapping("/goods")
public class GoodsController {
@ApiOperation(value = "商品添加功能")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResultVo addGoods() {
return null;
}
@ApiOperation(value = "商品删除功能")
@RequestMapping(value = "/delete/{gid}",method = RequestMethod.DELETE)
public ResultVo addGoods(@PathVariable("gid") int gid) {
System.out.println(gid);
return null;
}
@ApiOperation(value = "商品修改功能")
@RequestMapping(value = "/update",method = RequestMethod.PUT)
public ResultVo updateGoods() {
return null;
}
@ApiOperation(value = "商品查询功能")
@RequestMapping(value = "/get",method = RequestMethod.GET)
public ResultVo getGoods() {
return null;
}
}
还可以这样写: j将@RequestMapping(value = "/add", method = RequestMethod.POST)写成@PostMapping(value = "/add")
package com.xgsm.Fmmall.controller;
import com.xgsm.Fmmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@Api(value = "提供商品添加、修改、删除及查询的相关接口", tags = "商品管理")
@RequestMapping("/goods")
public class GoodsController {
@ApiOperation(value = "商品添加功能")
@PostMapping(value = "/add")
public ResultVo addGoods() {
return null;
}
@ApiOperation(value = "商品删除功能")
@DeleteMapping(value = "/delete/{gid}")
public ResultVo addGoods(@PathVariable("gid") int gid) {
System.out.println(gid);
return null;
}
@ApiOperation(value = "商品修改功能")
@PutMapping(value = "/update")
public ResultVo updateGoods() {
return null;
}
@ApiOperation(value = "商品查询功能")
@GetMapping(value = "/get")
public ResultVo getGoods() {
return null;
}
}
(3)、接口响应的资源表现形式采用json,由于返回的数据需要是json格式,这时候我们会使用到@ResponseBody注解,但是由于我们所有需要返回的数据都是json格式,所以我们可以在类的前面使用@ResponseBody注解进行声明。
但是在 RESTful风格中,提供了@RestController注解来替换@Controller、@ResponseBody
边栏推荐
- AtCoder Beginner Contest 260 E - At Least One
- 814. 二叉树剪枝
- Summary of the most complete MySQL data types in history (Part 2)
- Energy principle and variational method note 10: virtual displacement principle
- Analyze a ros2 cmakelists in detail Txt file
- C # implement setting expiration time for PDF documents
- 计网--网络层
- How to carry out the anti disclosure work of enterprise source code
- MATLAB中split函数使用
- Basic operation introduction of mongodb, creation and deletion of database / table, and data query
猜你喜欢
微信小程序开发流程
Eolink ——通过文档驱动,快速开发接口
mysql中的字段如何选择合适的数据类型呢?
Pango logos dual boot
The most complete summary of MySQL data types in history - (first)
优必选科技眼中的AI机器人时代
Seven best ways to get out of the psychological comfort zone
计网--网络层
2022年最新湖北建筑八大员(机械员)模拟考试题库及答案
Leetcode skimming: dynamic planning 02 (climbing stairs)
随机推荐
Eolink - quickly develop interfaces through document driven
etcdv3实战·常用操作模型封装及详细实现
Immunefi of hacker bounty hunter platform
走出心理舒适区的七个最佳方法
Halcon series (2): hyperboxes
国内外接口文档工具哪家强?
linux如何查询oracle错误日志
[wechat applet developer tool] × # initialize-error: Error: ENOENT: no such file or directory, open
IMU标定_随机误差的标定
什么是FastAPI异步框架?(全面了解)
Why should host reinforcement be done in industrial control industry
加载RainCitySpaces的标签信息并进行显示
研发中台拆分过程的一些心得总结
面试难题:分布式 Session 实现难点,这篇就够!
11-GuliMall 后台管理中商品系统的分类维护
mysql中的字段如何选择合适的数据类型呢?
IDEA 2022.2 正式发布,骚操作,跟不上了!
华泰证券开户开网银安全吗?需要去营业部办理吗
从零复现PyTorch版(3)
cron表达式详解