1、pom依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.5</version> </dependency> <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <version>3.0.2</version> </dependency> <!-- validator --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.4.Final</version> </dependency> <!-- tools --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>15.0</version> </dependency>
2、参数校验工具类:BeanValidator
import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.collections.MapUtils; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import java.util.*; /** * 参数校验工具类 * */ public class BeanValidator { private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); /** * 校验单个对象 * * @param t * @param groups * @param <T> * @return */ public static <T> Map<String, String> validate(T t, Class... groups) { Validator validator = validatorFactory.getValidator(); Set validateResult = validator.validate(t, groups); if (validateResult.isEmpty()) { return Collections.emptyMap(); } else { LinkedHashMap errors = Maps.newLinkedHashMap(); Iterator iterator = validateResult.iterator(); while (iterator.hasNext()) { ConstraintViolation violation = (ConstraintViolation)iterator.next(); errors.put(violation.getPropertyPath().toString(), violation.getMessage()); } return errors; } } /** * 校验对象列表 * * @param collection * @return */ public static Map<String, String> validateList(Collection<?> collection) { Preconditions.checkNotNull(collection); Iterator iterator = collection.iterator(); Map errors; do { if (!iterator.hasNext()) { return Collections.emptyMap(); } Object object = iterator.next(); errors = validate(object, new Class[0]); } while (errors.isEmpty()); return errors; } /** * 检验单个对象或者列表 * * @param first * @param objects * @return */ public static Map<String, String> validateObject(Object first, Object... objects) { if (objects != null && objects.length > 0) { return validateList(Lists.asList(first, objects)); } else { return validate(first, new Class[0]); } } /** * 通用参数校验 * * @param param * @throws ParamException */ public static void paramCheck(Object param) throws ParamException { Map<String, String> map = BeanValidator.validateObject(param); if (MapUtils.isNotEmpty(map)) { throw new ParamException(map.toString()); } } }
3、异常类:ParamException
import javax.annotation.Nullable; /** * 参数校验异常 * */ public class ParamException extends BaseException{ public ParamException(String message) { super(message); } public ParamException(String message, Throwable cause) { super(message, cause); } public ParamException(ErrorCode errorCode) { super(errorCode); } public ParamException(ErrorCode errorCode, @Nullable Object... messageArgs) { super(errorCode, messageArgs); } public ParamException(ErrorCode errorCode, @Nullable Throwable cause, @Nullable Object... messageArgs) { super(errorCode, cause, messageArgs); } }