一、新建两个校验规则接口
1.新增校验规则接口
/**
* @ClassName: InsertBeanValidated
* @Description: 校验分组-新增校验
**/
public interface InsertBeanValidated {
}
2.编辑校验规则接口
/**
* @ClassName: UpdateBeanValidated
* @Description: 校验分组-编辑校验
**/
public interface UpdateBeanValidated{
}
二、entity实体类增加分组校验规则
@NotNull(message = "类型不能为空",groups = {UpdateBeanValidated.class})
private String type;
@NotNull(message = "名称不能为空",groups = {InsertBeanValidated.class})
private String name;
@NotNull(message = "性别不能为空")
private String sex;
三、controller层增加分组校验;多个用,隔开@Validated({UpdateBeanValidated.class, InsertBeanValidated.class}
@PostMapping(value = "/update", name = "测试")
public void update(@Validated({UpdateBeanValidated.class,Default.class}) @RequestBody Test test) {
}