如果我们通过不断的叠加对字段的判空总感觉很不方便,因此写了一个工具类,通过链式进行对obj的判断
如果按照传统方式,编码是这样的
public static void verify(RecallRequest request) {
if (request == null) {
throw new RuntimeException("请求不能为null");
}
if (StringUtils.isBlank(request.getImei())) {
throw new RuntimeException("请求imei不能为null");
}
if (request.getProvince() == 0) {
throw new RuntimeException("请求省不能为null");
}
if (request.getCity() == 0) {
throw new RuntimeException("请求市不能为null");
}
if (StringUtils.isBlank(request.getTab())) {
throw new RuntimeException("请求tab不能为null");
}
if (request.getSize() == 0) {
throw new RuntimeException("请求size不能为0");
}
}
现在是这样的
@Test
public void testCheck() {
try {
List<String> strings =
CheckUtils.create()
.addCheckEmpty("", "name is null")
.addCheckEmpty("", "gender is null")
.checkBatch();
if (ObjUtils.isNotEmpty(strings)) {
System.out.println(JSON.toJSONString(strings));
return;
}
System.out.println("通过");
} catch (Exception e) {
log.error("into the catch", e);
}
}
["name is null","gender is null"]