Java程序使用校验机制。(即脱离Spring使用)

1 定义工具类

package com.yufan.all.valid.beautifulResult;

import com.yufan.all.valid.dto.TestParent2Dto;
import org.apache.commons.collections.CollectionUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class ValidationUtil {

    private  static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    public static  ValidationResult validateEntity(T object,Class... groups){
        Set> set = validator.validate(object,groups);
        ValidationResult validationResult =  new ValidationResult();
        if(CollectionUtils.isNotEmpty(set)){
            validationResult.setHasErrors(true);
            Map errorMsg =  new HashMap<>();
            for (ConstraintViolation cv:set){
                System.out.println("123");
                errorMsg.put(cv.getPropertyPath().toString(),cv.getMessage());
            }
            validationResult.setErrorMsg(errorMsg);
        }
        return  validationResult;
    }


    public static  ValidationResult validateProperty(T object,String propertyName){
        ValidationResult validationResult =  new ValidationResult();


        Set> set = validator.validateProperty(object,propertyName, Default.class);
        if(CollectionUtils.isNotEmpty(set)){
            validationResult.setHasErrors(true);
            Map errorMsg =  new HashMap<>();
            for (ConstraintViolation cv:set){
                System.out.println("456");
                errorMsg.put(cv.getPropertyPath().toString(),cv.getMessage());
            }
            validationResult.setErrorMsg(errorMsg);
        }
        return  validationResult;
    }


    public static  ValidationResult validatePropertys(T object,String[] propertyNames){
        ValidationResult validationResult =  new ValidationResult();

        String allMsg="";

        for (String s :propertyNames){
            ValidationResult result = validateProperty(object, s);
            if(result.isHasErrors()){
                validationResult.setHasErrors(true);
            }
            Map errorMsg = result.getErrorMsg();
            String msg = errorMsg.get(s);
            allMsg+=s+":"+msg+";  ";
        }

        validationResult.setAllMsg(allMsg);

        return  validationResult;
    }


    public static void main(String[] args) throws Exception {
        TestParent2Dto testParent2Dto = new TestParent2Dto();


        //验证单个属性
        //ValidationResult validationResult = ValidationUtil.validateProperty(testParent2Dto, "enterpriseName");


        Class c = Class.forName(testParent2Dto.getClass().getName());
        Field[] fields = c.getDeclaredFields();
        Field.setAccessible(fields, true);
        String[] names= new String[fields.length];
        for(int i=0;i

2  定义公共返回

package com.yufan.all.valid.beautifulResult;

import java.util.Map;

public class ValidationResult {
    /**
     * 校验结果是否有错
     */
    private boolean hasErrors;

    /**
     * 校验错误信息
     */

    private Map errorMsg;

    private String allMsg;

    public boolean isHasErrors() {
        return hasErrors;
    }

    public void setHasErrors(boolean hasErrors) {
        this.hasErrors = hasErrors;
    }

    public Map getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(Map errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getAllMsg() {
        return allMsg;
    }

    public void setAllMsg(String allMsg) {
        this.allMsg = allMsg;
    }

    @Override
    public String toString() {
        return "ValidationResult{" +
                "hasErrors=" + hasErrors +
                ", errorMsg=" + errorMsg +
                ", allMsg='" + allMsg + '\'' +
                '}';
    }
}

 

你可能感兴趣的:(Java程序使用校验机制。(即脱离Spring使用))