当前位置:网站首页>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 .
边栏推荐
- Confused, I'm going to start running in the direction of [test]
- SimpleDateFormat线程不安全和DateTimeFormatter线程安全
- C language: realize the simple function of address book through structure
- Autoreleasepool problem summary
- oracle查看锁表语句、解锁方法
- Oracle用sql查询某张表的字段信息(字段类型、长度等)
- C language: some self realization of string functions
- 【ARIXV2204】Neighborhood attention transformer
- How to analyze fans' interests?
- Mysql基本查询
猜你喜欢

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

类和对象【中】

CentOS7安装MySQL5.7

Thinking on multi system architecture design

repackag failed: Unable to find main class

测试开发---自动化测试中的UI测试

MySQL date and time function, varchar and date are mutually converted

ByteBuffer.position 抛出异常 IllegalArgumentException

FreeRTOS personal notes - task notification

JMeter related knowledge sorting
随机推荐
About MySQL group_ What concat has to say
【ARIXV2204】Neighborhood attention transformer
The most detailed installation of windows10 virtual machine, install virtual machine by hand, and solve the problem that the Hyper-V option cannot be found in the home version window
lamda 获取当前循环数,AtomicInteger
HashSet add
(黑马)MYSQL初级-高级笔记(博主懒狗)
ArrayList多线程安全解决办法
regular expression
From the basic concept of micro services to core components - explain and analyze through an example
Database date types are all 0
block yandex bot
LocalDateTime去掉T,JSONField失效
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
What are the methods of array objects in Es5 and what are the new methods in ES6
PC端-bug记录
I've been in an outsourcing company for two years, and I feel like I'm going to die
IDEA使用dev-tool实现热部署
SMD component size metric English system corresponding description
restFul接口使用个人总结
Google browser cannot open localhost:3000. If you open localhost, you will jump to the test address