package com.vincent.rsf.common.domain; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import javax.validation.Validator; import java.util.List; import java.util.Map; import java.util.Set; /** * ${DESCRIPTION} * * @author Super-Admin * @CreateUser Ryan * @CreateTime 2018/6/14 16:12 * @ModifyUser YunTao.Zhou * @ModifiedTime 2018/6/14 16:12 * @Version 1.0 */ public class BeanValidators { /** * * @param validator * @param list * @param groups * @throws ConstraintViolationException */ public static void validateListWithException(Validator validator, List list, Class... groups) throws ConstraintViolationException { if(list.isEmpty()){ return; } Set constraintViolations = Sets.newHashSet(); for (Object o : list) { constraintViolations.addAll( validator.validate(o, groups)); if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } } } public static void validateArrayWithException(Validator validator, Object[] array, Class... groups){ BeanValidators.validateListWithException(validator, Lists.newArrayList(array),groups); } /** * 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static void validateWithException(Validator validator, Object object, Class... groups) throws ConstraintViolationException { Set constraintViolations = validator.validate(object, groups); if (!constraintViolations.isEmpty()) { throw new ConstraintViolationException(constraintViolations); } } /** * 辅助方法, 转换ConstraintViolationException中的Set中为List. */ public static List extractMessage(ConstraintViolationException e) { return extractMessage(e.getConstraintViolations()); } /** * 辅助方法, 转换Set为List */ @SuppressWarnings("rawtypes") public static List extractMessage(Set constraintViolations) { List errorMessages = Lists.newArrayList(); for (ConstraintViolation violation : constraintViolations) { errorMessages.add(violation.getMessage()); } return errorMessages; } /** * 辅助方法, 转换ConstraintViolationException中的Set为Map. */ public static Map extractPropertyAndMessage(ConstraintViolationException e) { return extractPropertyAndMessage(e.getConstraintViolations()); } /** * 辅助方法, 转换Set为Map. */ @SuppressWarnings("rawtypes") public static Map extractPropertyAndMessage(Set constraintViolations) { Map errorMessages = Maps.newHashMap(); for (ConstraintViolation violation : constraintViolations) { errorMessages.put(violation.getPropertyPath().toString(), violation.getMessage()); } return errorMessages; } /** * 辅助方法, 转换ConstraintViolationException中的Set为List. */ public static List extractPropertyAndMessageAsList(ConstraintViolationException e) { return extractPropertyAndMessageAsList(e.getConstraintViolations(), " "); } /** * 辅助方法, 转换Set为List. */ @SuppressWarnings("rawtypes") public static List extractPropertyAndMessageAsList(Set constraintViolations) { return extractPropertyAndMessageAsList(constraintViolations, " "); } /** * 辅助方法, 转换ConstraintViolationException中的Set为List. */ public static List extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) { return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator); } /** * 辅助方法, 转换Set为List. */ @SuppressWarnings("rawtypes") public static List extractPropertyAndMessageAsList(Set constraintViolations, String separator) { List errorMessages = Lists.newArrayList(); for (ConstraintViolation violation : constraintViolations) { errorMessages.add(violation.getPropertyPath() + separator + violation.getMessage()); } return errorMessages; } }