JSR303通用校验工具类

lijunyi2023-09-04javaJSR303

JSR303手动触发验证工具类

  • 适用于业务代码里需要手动触发 JSR303校验的场景
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ValidationUtil {
    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    /**
     * 校验对象
     *
     * @param object 待校验对象
     * @throws RuntimeException 校验不通过,则报RuntimeException异常
     */
    public static void validate(Object object) throws RuntimeException {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object);
        if (!constraintViolations.isEmpty()) {
            List<String> list = new ArrayList<>();
            for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
                String message = constraintViolation.getMessage();
                String propertyPath = constraintViolation.getPropertyPath().toString();
                list.add(propertyPath + ":" + message);
            }
            throw new RuntimeException(list.toString());
        }
    }

    /**
     * 校验对象中指定字段
     *
     * @param object       待校验对象
     * @param propertyName 属性名
     * @throws RuntimeException 校验不通过,则抛出异常
     */
    public static void validateProperty(Object object, String propertyName) throws RuntimeException {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validateProperty(object, propertyName);
        if (!constraintViolations.isEmpty()) {
            StringBuilder errorMessage = new StringBuilder();
            for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
                errorMessage.append(constraintViolation.getMessage()).append(",");
            }
            throw new RuntimeException(errorMessage.toString());
        }
    }

    /**
     * 校验对象中指定字段
     *
     * @param object       待校验对象
     * @param propertyName 属性名
     * @param groups       分组
     * @throws RuntimeException 校验不通过,则抛出异常
     */
    public static void validateProperty(Object object, String propertyName, Class<?>... groups) throws RuntimeException {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validateProperty(object, propertyName, groups);
        if (!constraintViolations.isEmpty()) {
            StringBuilder errorMessage = new StringBuilder();
            for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
                errorMessage.append(constraintViolation.getMessage()).append(",");
            }
            throw new RuntimeException(errorMessage.toString());
        }
    }

    /**
     * 校验对象中所有字段
     *
     * @param object 待校验对象
     * @throws RuntimeException 校验不通过,则抛出异常
     */
    public static void validateAllFields(Object object) throws RuntimeException {
        BindingResult result = org.springframework.validation.ValidatorUtils.getBindingResult(object);
        if (result.hasErrors()) {
            StringBuilder errorMessage = new StringBuilder();
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                errorMessage.append(fieldError.getDefaultMessage()).append(",");
            }
            throw new RuntimeException(errorMessage.toString());
        }
    }
}
Last Updated 2024/5/24 16:21:58