当前位置:网站首页>Personal summary of restful interface use
Personal summary of restful interface use
2022-07-28 05:28:00 【MusicCodeM】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
Preface
Tips : Here you can add the general content to be recorded in this article :
restFul style , Write the interface , Just use these four request methods 、
get Inquire about
post newly added
put modify
delete Delete
Be careful : Join in Rules of parameter output
Enter the reference : xxxDto
The ginseng :xxxVo

entity Store entity classes in
One controller Necessary in 5 Interface
Paging query list
details ( according to id Inquire about )
newly added
modify
Delete
get request
Situation 1 : Parameters have , But less , Not enough to use one Dto encapsulate .( Not recommended )
such as :name,age
http://localhost:8080/list? Parameter name = value & Parameter name = value &…
The format of the request sent by the front end :( It must be like this , Otherwise, the backend cannot receive data )
http://localhost:8080/list?name=1&age=2
The types of parameters passed in the request are strings , Interface , Not in String Received , Because of the type conversion
@GetMapping("/list")
public AjaxResult list(String name,Integer age ){
......
return AjaxResult.success(...);
}
Situation two : Input parameter is not id Of , Or more than one , It's all encapsulated in Dto in ( Suggest )
Dto, The class of the input parameter
benefits :
You can use validation parameters in classes , There are comments that can be verified , There is no need for us to annotate and verify . For example, whether the parameter is empty ,age It can't be negative , And so on .
details :https://blog.csdn.net/sunnyzyq/article/details/103527380
Enter the parameter class
public class xxxDto{
@NotNull
private String name;
@NotNull
private Integer age;
}
Back end interface
@GetMapping("/list")
public AjaxResult list(@Valid xxxDto){
......
return AjaxResult.success(...);
}
Front end requests :
http://localhost:8080/list?name=1&age=2
Situation three : Join as a
http://localhost:8080/findOne?id=xxxxx
@GetMapping("/findOne")
public AjaxResult findOne(String id ){
......
return AjaxResult.success(...);
}
http://localhost:8080/lfindOnd/123132131
@GetMapping("/findOne/{id}")
public AjaxResult findOne(@PathVariable("id") String id ){
......
return AjaxResult.success(...);
}
post request ( The input parameters are encapsulated into Dto in )
Be careful : Whether one or more attributes are inserted , It's all encapsulated in Dto in
Enter the parameter class
public class xxxDto{
@NotNull
private String name;
@NotNull
private Integer age;
}
Back end code
@PostMapping("/insertInfo")
public AjaxResult insertInfo(@Valid @RequestBody xxxDto dto){
.......
return ....
}
request :
localhost:8080/insertInfo Parameter in request body
put request
id Not written in the input parameter class , This can be done in Inserting and modifying , Use the same input parameter Dto
Enter the parameter class
public class xxxDto{
@NotNull
private String name;
@NotNull
private Integer age;
}
Back end code
@PostMapping("/updateInfo/{id}")
public AjaxResult insertInfo(@Valid @RequestBody xxxDto dto,@PathVariable("id") String id ){
.......
return ....
}
request :
localhost:8080/updateInfo/1231231 Parameter in request body
Pay attention to the following ways : Not recommended
Back end code :
@PutMapping("/updateStatus")
public AjaxResult updateStatus( Integer enableStatus,String id){
.......
return .....
}
In this case , Parameters cannot be obtained when they are sent to the request body ;
Send by :localhost:8080/updateStatus?enableStatus=1&id= 1232131231 Available to
Suggest : Use two or more parameters Dto encapsulation
xxxDto
public class xxxDto{
String id ;
Integer enableStatus;
}
@PutMapping("/updateInfo")
public AjaxResult updateInfo(@RequestBody xxxDto dto){
.......
return ....
}
One parameter :
@PutMapping("/updateInfo/{id}")
public AjaxResult updateInfo(@PathVariable("id" String id )){
.......
return ....
}
delete request
It's usually a biography id and ids These two kinds of
@DeleteMapping("/delete/{id}")
public AjaxResult deleteById(@PathVariable("id") String id){
...........
return ...;
}
@DeleteMapping("/delete/{ids}")
public AjaxResult deleteById(@PathVariable("id") String ids){
String[] idsStr = ids.split(",");
...........
return ...;
}
About Dto and Vo
In and out , Often contain Dealing with time
date : 2022-11-11 There is localDate 2022-11-11 11:11:11 Just use localDateTime
https://blog.csdn.net/qq_46539281/article/details/125683278
At the same time, it is necessary to add comments @DateFormatTIme and @JsonFormat annotation
@DateFormatTIme Use in Dto Join the reference
@JsonFormat Use in Out of the middle .
边栏推荐
- New modularity in ES6
- Using RAC to realize the sending logic of verification code
- 测试开发---自动化测试中的UI测试
- 2022 summer practice (PowerDesigner tutorial learning record) (first week)
- MySQL basic query
- Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"
- 21 day SQL punch in summary
- 2022 summer practice (first week)
- PC端-bug记录
- How to successfully test php7.1 connecting to sqlserver2008r2
猜你喜欢

IDEA使用dev-tool实现热部署

在ruoyi生成的对应数据库的代码 之后我该怎么做才能做出下边图片的样子

FreeRTOS personal notes - task notification

SSLError

mysql 为查询结果增加序号
![Classes and objects [medium]](/img/0a/955d00d63f06e7e15e946599628edf.png)
Classes and objects [medium]

2022 summer practice (PowerDesigner tutorial learning record) (first week)

CentOS7安装MySQL5.7

PC side bug record

Microservice failure mode and building elastic system
随机推荐
凛冬已至,程序员该怎么取暖
正则表达式
CentOS7安装MySQL5.7
From the basic concept of micro services to core components - explain and analyze through an example
The research group passed the thesis defense successfully
11.< tag-动态规划和子序列, 子数组>lt.115. 不同的子序列 + lt. 583. 两个字符串的删除操作 dbc
MySQL date and time function, varchar and date are mutually converted
Redis 之布隆过滤器
I interviewed a 38 year old programmer and refused to work overtime
多系统架构设计思考
【CVPR2022】On the Integration of Self-Attention and Convolution
oracle查看锁表语句、解锁方法
21 day SQL punch in summary
ssm项目快速搭建项目配置文件
【内功心法】——函数栈帧的创建和销毁(C实现)
New methods and features of ES6 built-in objects
Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid
Advanced assignment method of ES6 -- Deconstruction assignment
Autoreleasepool problem summary
PC端-bug记录