文章目录

  • 前言
  • 一个controller中必须的5个接口
  • get 请求
  • 情况一:参数有,但是少,不足以用一个Dto进行封装。(不建议这样)
  • 情况二:入参不是id的,或者大于一个的,都封装到Dto中(建议)
  • 情况三:入参为一个
  • post请求(入参都封装到Dto中)
  • put请求
  • delete请求
  • 关于Dto 和Vo



前言

提示:这里可以添加本文要记录的大概内容:

restFul风格,写接口,就使用这四种请求方法、

get 查询
post 新增
put 修改
delete 删除

注意: 入参和 出参的规则

入参: xxxDto
出参:xxxVo

restful api接口规范 java restful接口规范 入参_post请求


entity中存放实体类


一个controller中必须的5个接口

  1. 分页查询列表
  2. 详情 (根据id查询)
  3. 新增
  4. 修改
  5. 删除

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不能是负数,等等的验证。



入参类

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 ....
    }



两个个参数:

@PutMapping("/updateInfo/{id}/{age}")
    public AjaxResult updateInfo(@PathVariable("id") String id ,@PathVariable("age") Interge age){
    	.......
        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

同时还要必添加的注解 @DateFormatTIme 和 @JsonFormat 注解

@DateFormatTIme 用在Dto 入参中
@JsonFormat 用在 出参中。