当前位置:网站首页>restFul接口使用个人总结
restFul接口使用个人总结
2022-07-28 05:17:00 【MusicCodeM】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
restFul风格,写接口,就使用这四种请求方法、
get 查询
post 新增
put 修改
delete 删除
注意: 入参和 出参的规则
入参: xxxDto
出参:xxxVo

entity中存放实体类
一个controller中必须的5个接口
分页查询列表
详情 (根据id查询)
新增
修改
删除
get 请求
情况一:参数有,但是少,不足以用一个Dto进行封装。(不建议这样)
比如:name,age
http://localhost:8080/list?参数名称=值&参数名=值&…
前端发送过来的请求的格式:(必须是这样的,不然后端接收不到数据)
http://localhost:8080/list?name=1&age=2
请求中传过来的参数的类型都是字符串,接口中,不是以String接收的,是因为进行了类型转换的
@GetMapping("/list")
public AjaxResult list(String name,Integer age ){
......
return AjaxResult.success(...);
}
情况二:入参不是id的,或者大于一个的,都封装到Dto中(建议)
Dto, 入参的类
好处:
在类中可以使用验证参数等,有注解可以进行验证,就不必我们注解验证了。比如参数是否为空啊,age不能是负数,等等的验证。
详情:https://blog.csdn.net/sunnyzyq/article/details/103527380
入参类
public class xxxDto{
@NotNull
private String name;
@NotNull
private Integer age;
}
后端接口
@GetMapping("/list")
public AjaxResult list(@Valid xxxDto){
......
return AjaxResult.success(...);
}
前端的请求:
http://localhost:8080/list?name=1&age=2
情况三:入参为一个
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请求(入参都封装到Dto中)
注意: 不管插入的是一个还是多个属性,都封装到Dto中
入参类
public class xxxDto{
@NotNull
private String name;
@NotNull
private Integer age;
}
后端代码
@PostMapping("/insertInfo")
public AjaxResult insertInfo(@Valid @RequestBody xxxDto dto){
.......
return ....
}
请求:
localhost:8080/insertInfo 参数在请求体中
put请求
id不写在入参类中,这样可以在 插入和修改中,使用同一个入参Dto
入参类
public class xxxDto{
@NotNull
private String name;
@NotNull
private Integer age;
}
后端代码
@PostMapping("/updateInfo/{id}")
public AjaxResult insertInfo(@Valid @RequestBody xxxDto dto,@PathVariable("id") String id ){
.......
return ....
}
请求:
localhost:8080/updateInfo/1231231 参数在请求体中
注意以下的方式: 不建议这样
后端代码:
@PutMapping("/updateStatus")
public AjaxResult updateStatus( Integer enableStatus,String id){
.......
return .....
}
这样的话,参数发送到请求体中也获取不到;
发送方式:localhost:8080/updateStatus?enableStatus=1&id= 1232131231 可以获取的到
建议: 两个参数及以上使用Dto封装
xxxDto
public class xxxDto{
String id ;
Integer enableStatus;
}
@PutMapping("/updateInfo")
public AjaxResult updateInfo(@RequestBody xxxDto dto){
.......
return ....
}
一个参数:
@PutMapping("/updateInfo/{id}")
public AjaxResult updateInfo(@PathVariable("id" String id )){
.......
return ....
}
delete请求
一般是传id 和ids 这两种
@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 ...;
}
关于Dto 和Vo
入参和出参,会经常是 包含 有关时间的处理
日期: 2022-11-11 就有localDate 2022-11-11 11:11:11 就用localDateTime
https://blog.csdn.net/qq_46539281/article/details/125683278
同时还要必添加的注解 @DateFormatTIme 和 @JsonFormat 注解
@DateFormatTIme 用在Dto 入参中
@JsonFormat 用在 出参中。
边栏推荐
- 数据库面试
- VMware Workstation 与 Device/Credential Guard 不兼容。禁用 Device/Credential Guard
- Class class added in ES6
- SimpleDateFormat线程不安全和DateTimeFormatter线程安全
- 为什么md5不可逆,却还可能被md5免费解密网站解密
- How does Alibaba use DDD to split microservices?
- Redis 之布隆过滤器
- 7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
- Mysql基本查询
- Scope, execution process and life cycle of bean
猜你喜欢

Simulink automatically generates STM32 code details

Flask Development & get/post request

You must configure either the server or JDBC driver (via the ‘serverTimezone)

FreeRTOS personal notes - task notification

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

架构设计思考之一(SSO设计)

自定义Json返回数据

First acquaintance with C language (2)

HashSet add

【内功心法】——函数栈帧的创建和销毁(C实现)
随机推荐
为什么md5不可逆,却还可能被md5免费解密网站解密
Implementation of simple upload function in PHP development
Specific differences between typedef and define
【内功心法】——函数栈帧的创建和销毁(C实现)
About MySQL group_ What concat has to say
Internal implementation principle of yymodel
Anaconda common instructions
Table image extraction based on traditional intersection method and Tesseract OCR
VMware Workstation 与 Device/Credential Guard 不兼容。禁用 Device/Credential Guard
【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution
MySQL basic query
Duoyu security browser will improve the security mode and make users browse more safely
Keil Chinese garbled code solution
Share several methods of managing flag bits in C program
The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
regular expression
2022 summer practice (PowerDesigner tutorial learning record) (first week)
SMD component size metric English system corresponding description
多系统架构设计思考
【计算机三级信息安全】信息安全保障概述