自定义实现注解校验工具类

package com.qiaoyatao.demo.util;
import com.alibaba.druid.sql.visitor.functions.Char;
import com.qiaoyatao.demo.constants.Symbol;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.*;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * 校验
 */
public class Validation {

    public static void validList(Object o, Class group) {
        if (o.getClass().getTypeName().equals(Symbol.LIST)) {
            List fields = (List) o;
            for (Object field : fields) {
                valid(field, group);
            }
        } else {
            valid(o, group);
        }
    }

    //对象校验(继承,子类都可以)
    public static void valid(Object o, Class group) {
        StringBuffer msg = new StringBuffer();
        for (Field field : BeanUtil.copyField(o.getClass())) {
            if (validType(field.getType())) {
                Object newClass = get(o, field);
                if (newClass != null) {
                    for (Field field1 : newClass.getClass().getDeclaredFields()) {
                        String fieldName = validField(get(o, field), group, field1);
                        if (fieldName != null) {
                            msg.append(fieldName + ",");
                        }
                    }
                }
                continue;
            }
            String fieldName = validField(o, group, field);
            if (fieldName != null) {
                msg.append(fieldName + ",");
            }
        }
        if (msg.toString().length() != 0) {
            throw new RuntimeException(substr(msg.toString()));
        }
    }

    private static String validField(Object o, Class group, Field field) {
        field.setAccessible(true);
        Null aNull = field.getAnnotation(Null.class);
        if (aNull != null && (get(o, field) != null && get(o, field).toString().length() != 0)) {
            return ck(aNull.groups(), group, field, aNull.message());
        }
        NotNull notNull = field.getAnnotation(NotNull.class);
        if (notNull != null && get(o, field) == null) {
            return ck(notNull.groups(), group, field, notNull.message());
        }
        NotBlank notBlank = field.getAnnotation(NotBlank.class);
        if (notBlank != null && (get(o, field) == null || get(o, field).toString().length() == 0)) {
            return ck(notBlank.groups(), group, field, notBlank.message());
        }
        Min min = field.getAnnotation(Min.class);
        if (min != null && (get(o, field) != null && Integer.parseInt(get(o, field).toString()) < min.value())) {
            return ck(min.groups(), group, field, min.message());
        }
        Max max = field.getAnnotation(Max.class);
        if (max != null && (get(o, field) != null && Integer.parseInt(get(o, field).toString()) > max.value())) {
            return ck(max.groups(), group, field, max.message());
        }
        Range range = field.getAnnotation(Range.class);
        if (range != null && (get(o, field) != null)) {
            int val = Integer.parseInt(get(o, field).toString());
            if (val < range.min() || val > range.max()) {
                return ck(range.groups(), group, field, range.message());
            }
        }
        Email email = field.getAnnotation(Email.class);
        if (email != null && (get(o, field) != null && get(o, field).toString().length() != 0)) {
            String val = get(o, field).toString();
            /*if (Pattern.matches(Symbol.EMAIL,val)){
                return ck(email.groups(), group, field,email.message());
            }*/
        }
        Past past = field.getAnnotation(Past.class);
        if (past != null && (get(o, field) != null && get(o, field).toString().length() != 0)) {
            if (new Date().before((Date) get(o, field))) {
                return ck(past.groups(), group, field, past.message());
            }
        }
        Future future = field.getAnnotation(Future.class);
        if (future != null && (get(o, field) != null && get(o, field).toString().length() != 0)) {
            if (new Date().after((Date) get(o, field))) {
                return ck(future.groups(), group, field, future.message());
            }
        }
        Length length = field.getAnnotation(Length.class);
        if (length != null && get(o, field) != null) {
            String val = get(o, field).toString();
            if (val.length() < length.min() || val.length() > length.max()) {
                return ck(length.groups(), group, field, length.message());
            }
        }
        AssertTrue aTrue = field.getAnnotation(AssertTrue.class);
        if (aTrue != null && get(o, field) != null) {
            boolean b = Boolean.parseBoolean(get(o, field).toString());
            if (!b) {
                return ck(aTrue.groups(), group, field, aTrue.message());
            }
        }
        AssertFalse aFalse = field.getAnnotation(AssertFalse.class);
        if (aFalse != null && get(o, field) != null) {
            boolean b = Boolean.parseBoolean(get(o, field).toString());
            if (b) {
                return ck(aFalse.groups(), group, field, aFalse.message());
            }
        }
        return null;
    }

    public static String ck(Class[] groups, Class group, Field field, String message) {
        if (group != null && groups.length != 0) {
            String name = groups(groups, group, field);
            if (name != null) {
               return name += ":" + message;
            }
            return null;
        } else if (group != null) {
            return null;
        } else if (group == null && groups.length != 0) {
            return null;
        }
        return field.getName() + ":" + message;
    }

    //获取组
    public static String groups(Class[] groups, Class group, Field field) {
        for (Class clazz : groups) {
            Class superclass = group.getSuperclass();//允许组继承
            if (clazz.equals(group) || clazz.equals(superclass)) {
                return field.getName();
            }
        }
        return null;
    }

    //获取值
    public static Object get(Object o, Field field) {
        try {
            field.setAccessible(true);
            return field.get(o);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    //判断是否为包装类型
    public static boolean validType(Class type) {
        if (type.equals(String.class) || type.equals(Set.class)) {
            return false;
        } else if (type.equals(Integer.class) || type.equals(int.class)) {
            return false;
        } else if (type.equals(BigDecimal.class) || type.equals(Date.class)) {
            return false;
        } else if (type.equals(Double.class) || type.equals(double.class)) {
            return false;
        } else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
            return false;
        } else if (type.equals(Byte.class) || type.equals(byte.class)) {
            return false;
        } else if (type.equals(Short.class) || type.equals(short.class)) {
            return false;
        } else if (type.equals(Char.class) || type.equals(char.class)) {
            return false;
        } else if (type.equals(List.class) || type.equals(Map.class)) {
            return false;
        }else if (type.equals(HashMap.class) || type.equals(HashSet.class)) {
            return false;
        }
        return true;
    }
    //截取字符串
    private static String substr(String str) {
        return str.substring(0, str.length() - 1);
    }
}

分组校验创建组对象,组对象可以继承

package com.qiaoyatao.demo.group;

public class DelGroup{
}
public class AddUserGroup extends DelGroup{
}

测试

@PostMapping(value = "test")
    @Group(value = AddUserGroup.class)
    public UserDTO test(@RequestBody   UserDTO dto) {
//        Validation.valid(dto, DelGroup.class);
        return dto;
    }

 

你可能感兴趣的:(java,java注解)