<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
<exclusions>
<exclusion>
<artifactId>validation-api</artifactId>
<groupId>javax.validation</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>9.0.33</version>
</dependency>
非工具类使用:
使用注解@Valid
工具类使用:
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;
public class ValidatorUtil {
/**
* 验证器
*/
private static Validator validator;``
static {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}
/**
* 验证方法
* <p>
* 同一个pojo类,可能会被多个controller使用验证,而每个controller的验证规则有不同,
* 这是就需要分组验证,其实就是几个要分组的空接口,指定属性A属于哪个组,属性B又属于
* 哪个组,这样在controller验证时就指定我要验证哪个组
* </p>
* @param object 被校验的对象
* @param groups 被校验的组
*/
public static void validateEntity(Object object, Class<?>... groups) throws RuntimeException{
// 用验证器执行验证,返回一个违反约束的set集合
Set<ConstraintViolation<Object>> violationSet = validator.validate(object, groups);
// 判断是否为空,空:说明验证通过,否则就验证失败
if(!violationSet.isEmpty()) {
// 获取第一个验证失败的属性
ConstraintViolation<Object> violation = violationSet.iterator().next();
// 抛出自定义异常
throw new RuntimeException(violation.getMessage());
}
}
}