阅读完需:约 1 分钟
Spring的Assert断言使用
org.springframework.util.Assert断言工具类
方法:
1:notNull(:)
notNull(Object object, String message) 和 notNull() 方法断言规则相反的方法是 isNull(Object object)/isNull(Object object, String message),它要求入参一定是 null;

2:isTrue(boolean expression) / isTrue(boolean expression, String message)
当 expression 不为 true 抛出异常;

3:notEmpty(Collection collection) / notEmpty(Collection collection, String message)
当集合未包含元素时抛出异常。

4:hasLength(String text) / hasLength(String text, String message)
当 text 为 null 或长度为 0 时抛出异常;

5: hasText(String text) / hasText(String text, String message)
text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;

/**
* Spring Assert用法
*/
public static void Assert(){
// String a=null;
//判断a是不是null 为null则抛出异常
// Assert.notNull(a,"a为null");
// int b=1;
//判断b等于1 不等于1就抛出异常;
// Assert.isTrue(b!=1,"b不等于1");
// List<String> list=new ArrayList<String>();
//当集合未包含元素时抛出异常。
// Assert.notEmpty(list,"list没有元素");
// String string="";
//当 string 为 null 或长度为 0 时抛出异常;
// Assert.hasLength(string,"text为null或者空");
String string="";
//text 不能为 null 且必须至少包含一个非空格的字符,否则抛出异常;
Assert.hasText(string,"string为null或者没有包含非空格字符");
}