当前位置:网站首页>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 .
边栏推荐
- Eccv2022 | 29 papers of Tencent Youtu were selected, including face security, image segmentation, target detection and other research directions
- mysql 为查询结果增加序号
- The research group passed the thesis defense successfully
- sql 查询list时两次的数据不一致,自动加上了limit
- Classes and objects [medium]
- 个人写的一个文件上传工具网站
- Reading notes of SMT practical guide 1
- Tips for using swiper (1)
- oracle查看锁表语句、解锁方法
- Flask Development & get/post request
猜你喜欢

Scope, execution process and life cycle of bean

mysql的日期与时间函数,varchar与date相互转换

After ruoyi generates the code corresponding to the database, what should I do to make the following image look like

多系统架构设计思考

【ARXIV2205】Inception Transformer

【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution

First acquaintance with C language (1)

ByteBuffer.position 抛出异常 IllegalArgumentException

ES6 new variable modifiers let and const, new basic data type symbol
![[slam] lvi-sam analysis - Overview](/img/66/f15f6f574807b9a783e9321c71d422.png)
[slam] lvi-sam analysis - Overview
随机推荐
Share several methods of managing flag bits in C program
SSLError
I've been in an outsourcing company for two years, and I feel like I'm going to die
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
Making RPM packages with nfpm
【CVPR2022】On the Integration of Self-Attention and Convolution
Interpretation of afnetworking4.0 request principle
regular expression
【单例模式】懒汉模式的线程安全问题
Melt cloud x chat, create a "stress free social" habitat with sound
【ARXIV2203】SepViT: Separable Vision Transformer
CentOS7安装MySQL5.7
BeanUtils.copyProperties无法复制不同List集合问题解决 Lists.transform函数
【idea插件神器】教你如何使用IDEA一键set实体类中所有属性
7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
Printf function of input and output function in C language
项目中问题合集
What are the methods of array objects in Es5 and what are the new methods in ES6
【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
block yandex bot