|
|
@@ -0,0 +1,41 @@
|
|
|
+package cn.hnthyy.thmz.common.validate;
|
|
|
+
|
|
|
+import cn.hnthyy.thmz.common.exception.BizException;
|
|
|
+
|
|
|
+import javax.validation.ConstraintViolation;
|
|
|
+import javax.validation.Validation;
|
|
|
+import javax.validation.Validator;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description:
|
|
|
+ * @Author:lihong
|
|
|
+ * @Date: 2023/8/15
|
|
|
+ */
|
|
|
+public class ValidatorUtils {
|
|
|
+ private static Validator validator;
|
|
|
+
|
|
|
+ static {
|
|
|
+ validator = Validation.buildDefaultValidatorFactory().getValidator();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验对象
|
|
|
+ * @param object 待校验对象
|
|
|
+ * @param groups 待校验的组
|
|
|
+ * @throws BizException 校验不通过,则报BizException异常
|
|
|
+ */
|
|
|
+ public static void validateEntity(Object object, Class<?>... groups)
|
|
|
+ throws BizException {
|
|
|
+ Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
|
|
|
+ if (!constraintViolations.isEmpty()) {
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
+ int index = 1;
|
|
|
+ for (ConstraintViolation<Object> constraint : constraintViolations) {
|
|
|
+ msg.append(index+"、").append(constraint.getMessage()).append("<br>");
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ throw new BizException(msg.toString(),-1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|