New file |
| | |
| | | package com.example.springboot.util; |
| | | |
| | | import org.hibernate.validator.HibernateValidator; |
| | | |
| | | import javax.validation.ConstraintViolation; |
| | | import javax.validation.Validation; |
| | | import javax.validation.Validator; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * hibernate-validator校验工具类 |
| | | * 参考文档:http://docs.jboss.org/hibernate/validator/5.4/reference/en-US/html_single/ |
| | | */ |
| | | public class ValidatorUtil { |
| | | private static Validator validator; |
| | | |
| | | static { |
| | | validator = Validation.byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator(); |
| | | } |
| | | |
| | | /** |
| | | * ValidatorUtil.validateEntity(role); |
| | | * 在controller接口中使用此方便校验实体参数的合法校验对象 |
| | | * |
| | | * @param object 待校验对象 |
| | | * @param groups 待校验的组 |
| | | */ |
| | | public static void validateEntity(Object object, Class<?>... groups) { |
| | | Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups); |
| | | if (!constraintViolations.isEmpty()) { |
| | | StringBuilder msg = new StringBuilder(); |
| | | for (ConstraintViolation<Object> constraint : constraintViolations) { |
| | | msg.append(constraint.getMessage()).append(","); |
| | | } |
| | | throw new RuntimeException(msg.toString()); |
| | | } |
| | | } |
| | | } |