Springboot @Validated 不同场景下,参数如何分别校验
作者:互联网
一、新建两个校验规则接口
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) {
}
标签:Springboot,UpdateBeanValidated,校验,private,分组,Validated,class,InsertBeanValidated 来源: https://blog.csdn.net/qq_34410726/article/details/120734576