分组校验步骤

  1. 定义分组 - 就是在实体类中添加指定分组的接口
  2. 定义校验项时指定归属的分组
  3. 校验时指定要校验的分组

具体实现步骤

首先在实体类底部添加校验接口

public interface Add extends Default {

}

public interface Update extends Default{


}

然后在实体类参数上面添加分组规则

@NotNull(groups = Update.class)
private Integer id;//主键ID

@NotEmpty(message = "categoryName不能为空",groups = {Add.class,Update.class})
private String categoryName;//分类名称

最后在controller接收参数中添加规则

@PutMapping
public Result update(@RequestBody @Validated({Category.Update.class}) Category category){
        categoryService.update(category);
        return Result.success();
}
最后修改:2024 年 01 月 10 日
如果觉得我的文章对你有用,请随意赞赏