diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java index 585599fda5..32c074f291 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java @@ -19,7 +19,7 @@ public enum EpmetErrorCode { ERR10007(10007, "当前帐号已在别处登录"), ERR10019(10019, "验证码不正确"), ERR401(401, "未授权"), - VALIDATE_ERROR(7000, "数据校验异常"), + INTERNAL_VALIDATE_ERROR(7000, "内部数据校验异常"), SERVER_ERROR(8000, "服务器开小差了..."), CANNOT_JOIN_GROUP(8001, "只有注册居民才可以加入小组"), CANNOT_CREATE_GROUP(8002, "只有党员和热心居民才能创建小组,请选择您的身份"), @@ -63,7 +63,9 @@ public enum EpmetErrorCode { OPER_ADD_CUSTOMER_ERROR(8705, "新增客户失败"), OPER_ADD_CUSTOMER_MANAGER_ERROR(8706, "新增客户管理员失败"), OPER_UPLOAD_FILE_OVER_SIZE(8707, "文件体积过大"), - OPER_UPLOAD_FILE_TYPE_ERROR(8708, "文件类型错误"); + OPER_UPLOAD_FILE_TYPE_ERROR(8708, "文件类型错误"), + + CUSTOMER_VALIDATE_ERROR(8999, "内部数据校验异常"); private int code; private String msg; diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java index ee165ef2e8..d368408919 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/ValidateException.java @@ -59,13 +59,13 @@ public class ValidateException extends RuntimeException { public ValidateException(String msg) { super(msg); - this.code = EpmetErrorCode.VALIDATE_ERROR.getCode(); + this.code = EpmetErrorCode.INTERNAL_VALIDATE_ERROR.getCode(); this.msg = msg; } public ValidateException(String msg, Throwable e) { super(msg, e); - this.code = EpmetErrorCode.VALIDATE_ERROR.getCode(); + this.code = EpmetErrorCode.INTERNAL_VALIDATE_ERROR.getCode(); this.msg = msg; } diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java index ac14e38ce6..c5ae8767a2 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/ValidatorUtils.java @@ -1,25 +1,26 @@ /** * Copyright (c) 2018 人人开源 All rights reserved. - * + *

* https://www.renren.io - * + *

* 版权所有,侵权必究! */ package com.epmet.commons.tools.validator; -import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.ValidateException; +import com.epmet.commons.tools.validator.group.CustomerClientShowGroup; import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.support.ResourceBundleMessageSource; +import org.springframework.util.CollectionUtils; import org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; -import java.util.Locale; -import java.util.Set; +import java.util.*; /** * hibernate-validator校验工具类 @@ -39,20 +40,67 @@ public class ValidatorUtils { /** * 校验对象 - * @param object 待校验对象 - * @param groups 待校验的组 - * @throws ValidateException 校验不通过,则报RenException异常 + * + * @param object 待校验对象 + * @param groups 待校验的组 + * @throws ValidateException 校验不通过,则报RenException异常 */ public static void validateEntity(Object object, Class... groups) throws ValidateException { + + List> customerShowGroups = new ArrayList<>(); + List> internalGroups = new ArrayList<>(); + + Arrays.asList(groups).forEach(g -> { + if (CustomerClientShowGroup.class.isAssignableFrom(g)) { + //如果派生自客户端显示分组,那么会优先校验,并且将错误提示给客户端,返回客户端可见的错误码 + customerShowGroups.add(g); + } else { + internalGroups.add(g); + } + }); + + // 1.校验客户端显示分组 + if (!CollectionUtils.isEmpty(customerShowGroups)) { + validate(EpmetErrorCode.CUSTOMER_VALIDATE_ERROR ,object, customerShowGroups.toArray(new Class[customerShowGroups.size()])); + } + + // 2.内部校验分组 + if (!CollectionUtils.isEmpty(internalGroups)) { + validate(EpmetErrorCode.INTERNAL_VALIDATE_ERROR, object, internalGroups.toArray(new Class[internalGroups.size()])); + } + } + + private static Validator getValidator() { Locale.setDefault(LocaleContextHolder.getLocale()); - Validator validator = Validation.byDefaultProvider().configure().messageInterpolator( - new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(getMessageSource()))) - .buildValidatorFactory().getValidator(); + return Validation + .byDefaultProvider() + .configure() + .messageInterpolator(new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(getMessageSource()))) + .buildValidatorFactory() + .getValidator(); + } + + /** + * 真正的校验方法 + * @param object + * @param errorCode + * @param groups + */ + private static void validate(EpmetErrorCode errorCode, Object object, Class... groups) { + Validator validator = getValidator(); Set> constraintViolations = validator.validate(object, groups); if (!constraintViolations.isEmpty()) { - ConstraintViolation constraint = constraintViolations.iterator().next(); - throw new ValidateException(constraint.getMessage()); + StringBuilder sb = new StringBuilder(); + Iterator> it = constraintViolations.iterator(); + while (it.hasNext()) { + ConstraintViolation constraint = it.next(); + sb.append(constraint.getMessage()).append(","); + } + + int lastDouhaoIndex = sb.lastIndexOf(","); + sb.replace(lastDouhaoIndex, lastDouhaoIndex + 1, ""); + throw new ValidateException(errorCode.getCode(),sb.toString()); } } } diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/group/CustomerClientShowGroup.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/group/CustomerClientShowGroup.java new file mode 100644 index 0000000000..a248ee61ce --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/group/CustomerClientShowGroup.java @@ -0,0 +1,8 @@ +package com.epmet.commons.tools.validator.group; + +/** + * 客户终端显示错误信息的校验分组,该分组下的错误会被提示到客户端界面显示 + * 其他具体接口中的校验分组接口应当继承自该接口 + */ +public interface CustomerClientShowGroup { +}