Validator验证常用工具类

BusinessException

public class BusinessException  extends RuntimeException
{
    private static final long serialVersionUID = 5009464957746664524L;

    private  String code;

    private  String message;

    public BusinessException(){}

    public BusinessException( String message)
    {
        super();
        this.code = "9999";
        this.message = message;
    }

    public BusinessException(String code, String message)
    {
        super();
        this.code = code;
        this.message = message;
    }

    public BusinessException (LocalError localError)
    {
        this(localError.getCode(), localError.getMessage());
    }


    public String getCode()
    {
        return code;
    }

    @Override
    public String getMessage()
    {
        return message;
    }

    public LocalError getLocalError()
    {
        final BusinessException businessException = this;
        return new LocalError() {
            @Override
            public String getMessage()
            {
                return businessException.getMessage();
            }

            //@Override
            @Override
            public String getCode()
            {
                return businessException.getCode();
            }
        };

    }
}

LocalError

public interface LocalError
{
    String getCode();

    String getMessage();
}

Validator


import cn.*.core.BusinessException;
import cn.*.core.LocalError;
import org.apache.commons.lang3.Validate;
import org.springframework.util.StringUtils;

import java.util.Collection;


public class Validator extends Validate
{
    public static <T> T notNull(final T object, LocalError localError)
    {
        if (object == null)
        {
            throw new BusinessException(localError);
        }
        return object;
    }

    public static <T> T isNull(final T object, LocalError localError)
    {
        if (object != null)
        {
            throw new BusinessException(localError);
        }
        return object;
    }

    public static <T extends CharSequence> T notBlank(final T chars, LocalError localError)
    {
        if (chars == null)
        {
            throw new BusinessException(localError);
        }
        if (!StringUtils.hasLength(chars))
        {
            throw new BusinessException(localError);
        }
        return chars;
    }

    public static <T extends CharSequence> T notEmpty(final T chars, LocalError localError)
    {
        if (chars == null)
        {
            throw new BusinessException(localError);
        }
        if (chars.length() == 0)
        {
            throw new BusinessException(localError);
        }
        return chars;
    }

    public static <T extends Collection> T notEmpty(final T collection, LocalError localError)
    {
        if (collection == null)
        {
            throw new BusinessException(localError);
        }
        if (collection.size() == 0)
        {
            throw new BusinessException(localError);
        }
        return collection;
    }

    public static void isNotZero(final int id, LocalError localError)
    {
        if (id == 0)
        {
            throw new BusinessException(localError);
        }
    }

    public static void isTrue(boolean expression, LocalError localError)
    {
        if (expression == false)
        {
            throw new BusinessException(localError);
        }
    }
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate Validator是一种基于注解的校验框架,用于验证JavaBean中的数据。它提供了一种简单易用的方式来确保数据的完整性和一致性,在实际开发中被广泛应用。 下面是一个校验工具类的示例: ```java import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import java.util.Set; public class ValidatorUtils { private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); /** * 校验对象 * * @param obj 待校验对象 * @param groups 待校验的组 * @throws Exception 校验不通过,则报Exception异常 */ public static void validateEntity(Object obj, Class<?>... groups) throws Exception { Set<ConstraintViolation<Object>> constraintViolations = validator.validate(obj, groups); if (!constraintViolations.isEmpty()) { StringBuilder msg = new StringBuilder(); for (ConstraintViolation<Object> constraintViolation : constraintViolations) { msg.append(constraintViolation.getMessage()).append("<br>"); } throw new Exception(msg.toString()); } } } ``` 使用示例: ```java public class User { @NotNull(message = "用户名不能为空") private String username; @NotNull(message = "密码不能为空") private String password; // getter and setter } public class Test { public static void main(String[] args) { User user = new User(); user.setUsername(null); user.setPassword(null); try { ValidatorUtils.validateEntity(user); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意事项: - 需要在JavaBean的属性上添加相应的注解; - 需要在校验工具类中使用`Validation.buildDefaultValidatorFactory().getValidator()`方法获取`Validator`对象; - 可以通过`groups`参数指定需要校验的组,如果不指定,则校验所有组的规则。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值