All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.cloud.config.web.BeanValidators Maven / Gradle / Ivy

The newest version!
package com.feingto.cloud.config.web;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.*;

/**
 * JSR303 Validator(Hibernate Validator)工具类
 * ConstraintViolation中包含propertyPath, message 和invalidValue等信息.
 * 提供了各种convert方法,适合不同的i18n需求:
 * 1. List, String内容为message
 * 2. List, String内容为propertyPath + separator + message
 * 3. Map
 *
 * @author longfei
 */
public class BeanValidators {
    /**
     * 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException.
     */
    public static void validateWithException(Validator validator, Object obj, Class... groups) throws ConstraintViolationException {
        Set> constraintViolations = validator.validate(obj, groups);
        if (!constraintViolations.isEmpty()) {
            throw new ConstraintViolationException(constraintViolations);
        }
    }

    /**
     * 辅助方法, 转换ConstraintViolationException中的Set中为List.
     */
    public static List extractMessage(ConstraintViolationException e) {
        return extractMessage(e.getConstraintViolations());
    }

    /**
     * 辅助方法, 转换Set为List
     */
    public static List extractMessage(Set> constraintViolations) {
        List errorMessages = new ArrayList<>();
        for (ConstraintViolation violation : constraintViolations) {
            errorMessages.add(violation.getMessage());
        }
        return errorMessages;
    }

    /**
     * 辅助方法, 转换ConstraintViolationException中的Set为Map.
     */
    public static Map extractPropertyAndMessage(ConstraintViolationException e) {
        return extractPropertyAndMessage(e.getConstraintViolations());
    }

    /**
     * 辅助方法, 转换Set为Map.
     */
    public static Map extractPropertyAndMessage(Set> constraintViolations) {
        Map errorMessages = new HashMap<>();
        for (ConstraintViolation violation : constraintViolations) {
            errorMessages.put(violation.getPropertyPath().toString(), violation.getMessage());
        }
        return errorMessages;
    }

    /**
     * 辅助方法, 转换ConstraintViolationException中的Set为List.
     */
    public static List extractPropertyAndMessageAsList(ConstraintViolationException e) {
        return extractPropertyAndMessageAsList(e.getConstraintViolations(), " ");
    }

    /**
     * 辅助方法, 转换Set为List.
     */
    public static List extractPropertyAndMessageAsList(Set> constraintViolations) {
        return extractPropertyAndMessageAsList(constraintViolations, " ");
    }

    /**
     * 辅助方法, 转换ConstraintViolationException中的Set为List.
     */
    public static List extractPropertyAndMessageAsList(ConstraintViolationException e, String separator) {
        return extractPropertyAndMessageAsList(e.getConstraintViolations(), separator);
    }

    /**
     * 辅助方法, 转换Set为List.
     */
    public static List extractPropertyAndMessageAsList(Set> constraintViolations, String separator) {
        List errorMessages = new ArrayList<>();
        for (ConstraintViolation violation : constraintViolations) {
            errorMessages.add(violation.getPropertyPath() + separator + violation.getMessage());
        }
        return errorMessages;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy