校验源码
```java
// controller统一写法
@RequestMapping("XX")
// @RequiresPermissions("product:category:update")
public R XXX(@Validated(XXX.class) @RequestBody XXX xxx){
}
// 即在想要校验的类之前加一个@Validated注解, 在注解中声明校验分组即可
```
```java
/**
* 商品三级分类
*
* @author JiangJunJie
* @email a6691893@163.com
* @date 2023-06-29 00:08:28
*/
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 分类id
*/
@TableId
@Null(message = "catId必须为空",
groups = AddGroup.class)
@NotNull(message = "catId不能为空",
groups = {UpdateGroup.class, UpdateSortGroup.class})
private Long catId;
/**
* 分类名称 11
*/
@NotBlank(message = "name不能为空",
groups = {AddGroup.class, UpdateGroup.class})
@Null(message = "只能移动哦!", groups = UpdateSortGroup.class)
private String name;
/**
* 父分类id
*/
@NotNull(message = "父ID不能为空",
groups = {AddGroup.class, UpdateSortGroup.class})
@Null(message = "只能更新哦!", groups = UpdateGroup.class)
private Long parentCid;
/**
* 层级
*/
@NotNull(message = "层级不能为空",
groups = {AddGroup.class, UpdateSortGroup.class})
@Null(message = "只能更新哦!", groups = UpdateGroup.class)
private Integer catLevel;
/**
* 是否显示[0-不显示,1显示]
*/
@ListValue(value = {0, 1},
groups = {AddGroup.class, UpdateGroup.class})
@NotNull(message = "显示状态必须存在",
groups = {AddGroup.class, UpdateGroup.class})
@Null(message = "只能移动哦!", groups = UpdateSortGroup.class)
@TableLogic
private Integer showStatus;
/**
* 排序
*/
@NotNull(message = "必须要有优先级",
groups = {AddGroup.class, UpdateSortGroup.class})
@Null(message = "只能更新哦!", groups = UpdateGroup.class)
private Integer sort;
/**
* 图标地址
*/
@NotBlank(message = "图标不能为空",
groups = {AddGroup.class, UpdateGroup.class})
@URL(message = "必须是一个合法的图片URL",
groups = {AddGroup.class, UpdateGroup.class})
@Null(message = "只能移动哦!", groups = UpdateSortGroup.class)
private String icon;
/**
* 计量单位
*/
@NotBlank(message = "单位不能为空",
groups = {AddGroup.class, UpdateGroup.class})
@Null(message = "只能移动哦!", groups = UpdateSortGroup.class)
private String productUnit;
/**
* 商品数量
*/
// TODO: 校验待修改
private Integer productCount;
/**
* 子分类
*/
@Null(message = "只能通过逻辑添加, 不允许任何非法添加",
groups = {AddGroup.class, UpdateSortGroup.class, UpdateGroup.class})
@TableField(exist = false)
@JsonInclude(JsonInclude.Include.NON_EMPTY) // 相当于这个不是空集合, 才能放在JSON中
private List<CategoryEntity> children;
}
```
****
```java
/**
* 品牌
*
* @author JiangJunJie
* @email a6691893@163.com
* @date 2023-06-29 00:08:28
*/
@Data
@TableName("pms_brand")
public class BrandEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 品牌id
*/
@TableId
@Null(message = "品牌ID必须为空",
groups = AddGroup.class) // 添加的时候, 一定不能写ID
@NotNull(message = "品牌ID不能为空",
groups = {UpdateGroup.class, UpdateStatusGroup.class}) // 更新的时候, 一定要写ID
private Long brandId;
/**
* 品牌名
*/
@NotBlank(message = "必须写品牌名字",
groups = {AddGroup.class, UpdateGroup.class}) // 添加或普通修改的时候, 名字一定要有
@Null(message = "只能改变状态哦!", groups = UpdateStatusGroup.class)
private String name;
/**
* 品牌logo地址
*/
@URL(message = "logo必须是一个合法图片地址",
groups = {AddGroup.class, UpdateGroup.class}) // 添加或普通修改的时候, 图片一定要有
@NotBlank(message = "logo必须要有图片地址",
groups = {AddGroup.class, UpdateGroup.class})
@Null(message = "只能改变状态哦!", groups = UpdateStatusGroup.class)
private String logo;
/**
* 介绍
*/
@NotBlank(message = "必须写相关的描述",
groups = {AddGroup.class, UpdateGroup.class}) // 添加或普通修改的时候, 图片一定要有
@Null(message = "只能改变状态哦!", groups = UpdateStatusGroup.class)
private String descript;
/**
* 显示状态[0-不显示;1-显示]
*/
@ListValue(value = {0, 1},
groups = {AddGroup.class, UpdateGroup.class, UpdateStatusGroup.class})
@NotNull(message = "必须写显示状态",
groups = {AddGroup.class, UpdateGroup.class, UpdateStatusGroup.class}) // 任何的添加和更新都不能为空
private Integer showStatus;
/**
* 检索首字母
*/
@NotBlank(message = "必须写检索首字母",
groups = {AddGroup.class, UpdateGroup.class})
@Pattern(regexp = "^[a-zA-Z]$", message = "首字母只能由一个字母组成",
groups = {AddGroup.class, UpdateGroup.class}) // 添加或普通修改的时候, 图片一定要有
@Null(message = "只能改变状态哦!", groups = UpdateStatusGroup.class)
private String firstLetter;
/**
* 排序
*/
@NotNull(message = "优先级不能为空",
groups = {AddGroup.class, UpdateGroup.class})
@Min(value = 0, message = "必须是大于等于0的整数",
groups = {AddGroup.class, UpdateGroup.class}) // 添加或普通修改的时候, 图片一定要有
@Null(message = "只能改变状态哦!", groups = UpdateStatusGroup.class)
private Integer sort;
}
```
```java
/**
* Copyright 2023 bejson.com
*/
package com.junjie.bitmall.product.vo;
import com.junjie.bitmall.common.valid.anno.CollectionURL;
import com.junjie.bitmall.common.valid.anno.ListValue;
import com.junjie.bitmall.common.valid.group.AddGroup;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.URL;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.math.BigDecimal;
import java.util.List;
/**
* 代码生成方式<br/>
* 1. 点击链接:<a href="https://www.bejson.com/json2javapojo/new/">JSON转换实体类</a><br/>
* 2. 将前端页面将要发送请求的JSON载体CV进去<br/>
* 3. 填入Java类名和包名<br/>
* 4. 点击生成并下载, 然后拷贝到项目中<br/>
*/
@Data
@Accessors(chain = true)
public class SpuSaveVO {
@NotBlank(message = "SPU名字不能为空串",
groups = AddGroup.class)
private String spuName;
@NotBlank(message = "SPU描述信息不能为空串",
groups = AddGroup.class)
private String spuDescription;
@NotNull(message = "所属的分类不能为空",
groups = AddGroup.class)
private Long catalogId;
@NotNull(message = "所属的品牌不能为空",
groups = AddGroup.class)
private Long brandId;
@Positive(message = "重量不合法",
groups = AddGroup.class)
private BigDecimal weight;
@ListValue(value = {0, 1, 2},
message = "状态必须是[0,2]",
groups = AddGroup.class)
@NotNull(message = "必须要有发布状态",
groups = AddGroup.class)
private Integer publishStatus;
@NotEmpty(message = "SPU描述图片不能为空",
groups = AddGroup.class)
@CollectionURL(message = "SPU描述图片必须是一个合法的URL地址",
groups = AddGroup.class)
private List<String> decript;
@NotEmpty(message = "SPU图片集不能为空",
groups = AddGroup.class)
@CollectionURL(message = "SPU图片集必须是一个合法的URL地址",
groups = AddGroup.class)
private List<String> images;
@NotNull(message = "SPU的积分信息必须存在",
groups = AddGroup.class)
// TODO: 校验功能待进一步完善
private Bounds bounds;
@NotEmpty(message = "SPU规格参数必须存在",
groups = AddGroup.class)
// TODO: 校验功能待进一步完善
private List<BaseAttrs> baseAttrs;
@NotEmpty(message = "SPU对应的SKU必须存在",
groups = AddGroup.class)
private List<Skus> skus;
}
```
[自定义校验](doc:CLFBrD0z)