springboot分组校验
把校验项进行归类分组,在完成不同的功能的时候,校验组中的校验项
1.定义分组
2.定义校验项时指定归属的分组
3.校验时指定要校验的分组
@NotNull中group的添加以及
public interface Add{ }
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.apache.ibatis.annotations.Update;
import java.time.LocalDateTime;
@Data
public class Category {
@NotNull(groups = Update.class)
private Integer id;//主键ID
@NotEmpty(groups = {Add.class, Update.class})
private String categoryName;//分类名称
@NotEmpty(groups = {Add.class, Update.class})
private String categoryAlias;//分类别名
private Integer createUser;//创建人ID
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;//创建时间
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;//更新时间
public interface Add{
}
public interface Update{
}
}
@Validated(Category.Update.class)
@PutMapping
public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
categoryService.update(category);
return Result.success();
}
注意:定义校验项时如果没有制定分组,则属于Default分组,分组可以继承
原文地址:https://blog.csdn.net/2301_80256000/article/details/142798081
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!