当前位置:网站首页>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 .
边栏推荐
- Digital twin technology creates visual application of smart mine
- BigDecimal 进行四舍五入 四舍六入和保留两位小数
- MySQL basic query
- Oracle用sql查询某张表的字段信息(字段类型、长度等)
- C language characters and strings
- I interviewed a 38 year old programmer and refused to work overtime
- regular expression
- How to successfully test php7.1 connecting to sqlserver2008r2
- 【CVPR2022】On the Integration of Self-Attention and Convolution
- Specific differences between typedef and define
猜你喜欢

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

VMware Workstation 与 Device/Credential Guard 不兼容。禁用 Device/Credential Guard

Duoyu security browser will improve the security mode and make users browse more safely

MySQL practice 45 lectures

Thinking on multi system architecture design

Struct模块到底有多实用?一个知识点立马学习

Scanf function of input and output function in C language

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

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

【ARXIV2205】Inception Transformer
随机推荐
mybaties foreach多选查询,index循环,取消and/or标签
7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
block yandex bot
Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid
Microservice failure mode and building elastic system
Antd setfieldsvalue warning problem cannot use 'setfieldsvalue' until you use 'getfielddecorator' or
What are the methods of array objects in Es5 and what are the new methods in ES6
Duoyu security browser will improve the security mode and make users browse more safely
mysql中使用list作为参数进行查询
注册中心服务eureka 切换到 nocas遇到的问题
FreeRTOS个人笔记-任务通知
Using RAC to realize the sending logic of verification code
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
[internal mental skill] - creation and destruction of function stack frame (C implementation)
regular expression
在ruoyi生成的对应数据库的代码 之后我该怎么做才能做出下边图片的样子
First acquaintance with C language (2)
HashSet add
个人写的一个文件上传工具网站
【idea插件神器】教你如何使用IDEA一键set实体类中所有属性