<!--引入依赖-->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
//校验实体类
public static<T> String classMustIsNot(T t){
try {
ValidatorFactory factory= Validation.buildDefaultValidatorFactory();
Validator validator=factory.getValidator();
Set<ConstraintViolation<T>> constraintValidators=validator.validate(t);
Iterator<ConstraintViolation<T>> iterator = constraintValidators.iterator();
ConstraintViolation<T> next = iterator.next();
String message = next.getMessage();