Spring 断言工具类 Assert的常用方法

import org.springframework.util.Assert;

Assert断言工具类,通常用于数据合法性检查.、

自己平常使用的方法:

1.Assert.hasText(message.getSubject(), "主题不能为空");  
参数不为null,并且包含至少一个不为空的字符
str != null && str.length() > 0

2. Assert.isTrue(!CollectionUtils.isEmpty(message.getReceiverList()), "至少有一个接收人");

参数为true
public static void isTrue(boolean expression, String message) {
    if (!expression) {
        throw new IllegalArgumentException(message);
    }
}

3.Assert.notNull(message.getContentType(), "内容类型不能为空");

参数不为null
public static void notNull(Object object, String message) {
    if (object == null) {
        throw new IllegalArgumentException(message);
    }
}

4.Assert.notEmpty(Collection collection, "collection must not be empty")  

   集合不为空

public static void notEmpty(Collection<?> collection, String message) {
    if (CollectionUtils.isEmpty(collection)) {
        throw new IllegalArgumentException(message);
    }
}

后续有使用,再补充

猜你喜欢

转载自blog.csdn.net/oschina_40730821/article/details/88820391
今日推荐