当前位置:网站首页>Restful URL design specification
Restful URL design specification
2022-07-22 11:00:00 【Ink in leisure time】
In projects with separate front and rear ends , Between the front and back end is the interface for request and response , When the back end provides a request to the front end, it will expose a URL;URL Your design cannot be arbitrary , It needs to comply with certain design specifications ---RESTful
Catalog
One 、RESTful brief introduction :
3、 ... and 、RESTful- Interface :
One 、RESTful brief introduction :
RESTful It's a kind of web api Standards for , It's a kind of url design code .
Two 、 RESTful standard :
RESTful Specifications need to follow three rules , They are as follows :
- Every URL The request path represents the only resource on the server
- Different requests are used to represent different operations
- The resource expression form of interface response adopts json
(1)、 Every URL The request path represents the only resource on the server ; If delete , In traditional url In design , The same url route (http://localhost:8080/goods/delete), So not satisfied RESTful The first rule to be followed in the specification , And in the RESTful In the design, each URL The request path represents the only resource on the server ( The path is unique )
Conventional url Design :
http://localhost:8080/goods/delete?goodId=1 goods 1
http://localhost:8080/goods/delete?goodId=2 goods 2
RESTful Design :
http://localhost:8080/goods/delete/1 goods 1
http://localhost:8080/goods/delete/2 goods 2
@ApiOperation(value = " Product deletion function ")
@RequestMapping(value = "/delete/{gid}",method = RequestMethod.DELETE)
public ResultVo addGoods(@PathVariable("gid") int gid) {
System.out.println(gid);
return null;
}
Open the interface document :http://localhost:8080/doc.html debug
Console view results : Output “1”, The request is successful ~
(2)、 Different requests are used to represent different operations .springmvc Yes RESTful Style provides good support , Before we define an interface url when , Can pass @RequestMapping(value = "/delete/{gid}",method = RequestMethod.DELETE) Specify request method . You can also use specific request mode annotations for measurement
post | add to |
get | Inquire about |
put | modify |
delete | Delete |
option | preview |
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 = " Provide product addition 、 modify 、 Delete and query related interfaces ", tags = " Commodity management ")
@RequestMapping("/goods")
public class GoodsController {
@ApiOperation(value = " Product add function ")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ResultVo addGoods() {
return null;
}
@ApiOperation(value = " Product deletion function ")
@RequestMapping(value = "/delete/{gid}",method = RequestMethod.DELETE)
public ResultVo addGoods(@PathVariable("gid") int gid) {
System.out.println(gid);
return null;
}
@ApiOperation(value = " Product modification function ")
@RequestMapping(value = "/update",method = RequestMethod.PUT)
public ResultVo updateGoods() {
return null;
}
@ApiOperation(value = " Commodity inquiry function ")
@RequestMapping(value = "/get",method = RequestMethod.GET)
public ResultVo getGoods() {
return null;
}
}
You can also write : j take @RequestMapping(value = "/add", method = RequestMethod.POST) It's written in @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 = " Provide product addition 、 modify 、 Delete and query related interfaces ", tags = " Commodity management ")
@RequestMapping("/goods")
public class GoodsController {
@ApiOperation(value = " Product add function ")
@PostMapping(value = "/add")
public ResultVo addGoods() {
return null;
}
@ApiOperation(value = " Product deletion function ")
@DeleteMapping(value = "/delete/{gid}")
public ResultVo addGoods(@PathVariable("gid") int gid) {
System.out.println(gid);
return null;
}
@ApiOperation(value = " Product modification function ")
@PutMapping(value = "/update")
public ResultVo updateGoods() {
return null;
}
@ApiOperation(value = " Commodity inquiry function ")
@GetMapping(value = "/get")
public ResultVo getGoods() {
return null;
}
}
(3)、 The resource expression form of interface response adopts json, Because the returned data needs to be json Format , At this time, we will use @ResponseBody annotation , But because all the data we need to return is json Format , So we can use in front of the class @ResponseBody Make a statement with comments .
But in RESTful In style , Provides @RestController Note to replace @Controller、@ResponseBody
边栏推荐
- Response response byte data
- 易方达基金上买基金安全吗,我要做基金定投
- Chapter1 Beginning Bash
- Is it safe to buy funds on e fund? I want to make a fixed investment in the fund
- MongoDB的基本操作介绍、数据库/表的创建和删除、数据的查询
- ES6常用语法
- What are the five standards for the dual prevention mechanism of hazardous chemical enterprises and what are the contents
- [cloud native | learn kubernetes from scratch] VII. Resource list and namespace
- pango logos 双启动
- C # realize the conversion of Chinese characters to Pinyin
猜你喜欢
What are the five standards for the dual prevention mechanism of hazardous chemical enterprises and what are the contents
MongoDB的基本操作介绍、数据库/表的创建和删除、数据的查询
MATLAB 自然样条函数(约束两端斜率)
Keyframesextractutils Py, dynamically supports three framing methods, key parameters can be configured, and the code has been optimized for better effect and performance.
Pango logos dual boot
面试复盘四
Dota2参议院[贪心与队列]
计网--传输层
How to carry out the anti disclosure work of enterprise source code
机房动环监控系统的功能,动环监控系统的主要功能
随机推荐
BigDecimal精度的哪些坑
单页面引用记录上一次滑动的位置
Is it safe for Huatai Securities to open an account online, and can VIP accounts be handled
Response响应字节数据
【公开课预告】:云视频会议系统私有化实践
Reproduce pytorch version from zero (3)
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
可控文本生成系列-A Survey of Controllable Text Generation using Transformer-based Pre-trained
基于云原生的私有化 PaaS 平台交付实践
pango logos 双启动
C#实现汉字转拼音
华泰证券开户安全性好吗?手续费可以做到万一吗?
What are the five standards for the dual prevention mechanism of hazardous chemical enterprises and what are the contents
List分页方法
[public class preview]: cloud video conference system privatization practice
危化品化工企业双重预防机制五有标准是什么包括哪些内容
MATLAB中split函数使用
计网--网络层
机房动环监控系统的功能,动环监控系统的主要功能
易方达基金上买基金安全吗,我要做基金定投