断言某些对象或值是否符合规定,否则抛出异常。经常用于做变量检查
Result 类可参考《Spring boot统一包装返回包装类、捕获异常(处理)、记录请求和响应日志》
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
public class Assert extends cn.hutool.core.lang.Assert {
/**
* 默认提示:参数错误
*/
public static void isAssert() {
throw new IllegalArgumentException(Result.CodeEnum.PARAM_ERROR.getDesc());
}
public static void isAssert(Boolean isException, Integer resultCode, String errorMsgTemplate, Object... params) {
if (isException) {
throw new BusinessException(resultCode, errorMsgTemplate, params);
}
}
public static void isAssert(Boolean isException, Result.CodeEnum resultCodeEnum, String errorMsgTemplate, Object... params) {
if (isException) {
throw new BusinessException(resultCodeEnum, errorMsgTemplate, params);
}
}
/**
* 断言
*
* @param errorMsgTemplate 错误消息模板,变量使用{}表示
*/
public static void isAssert(String errorMsgTemplate, Object... params) {
throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params));
}
/**
* 断言
*
* @param errorMsgTemplate 错误消息模板,变量使用{}表示
*/
public static void isEmpty(String errorMsgTemplate, Object... params) {
throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params));
}
public static <E, T extends Iterable<E>> T isEmpty(T collection, String errorMsgTemplate, Object... params) throws IllegalArgumentException {
if (CollUtil.isNotEmpty(collection)) {
throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params));
}
return collection;
}
/**
* 验证值是否包含指定的
*
* @param source
* @param map key:字段名称,value 字段的值需要 contains 的值
*/
public static void isContain(String source, Map<String, Set<String>> map) {
if (StrUtil.isBlank(source) || CollUtil.isEmpty(map)) {
return;
}
String key = map.keySet().stream().findFirst().get();
Set<String> targetSets = map.get(key);
isTrue(targetSets.contains(source), " {}数据错误:正确选项{}", key, targetSets);
}
/**
* 通过注解校验
*
* @param obj
*/
public static void validByAnnotate(Object obj) {
Class<?> clazz = obj.getClass();
for (Field f : clazz.getDeclaredFields()) {
try {
Field field = clazz.getDeclaredField(f.getName());
field.setAccessible(true);
notBlankByAnnotate(field, obj);
notNullByAnnotate(field, obj);
notEmptyByAnnotate(field, obj);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalArgumentException(e.getMessage());
} catch (NoSuchFieldException e) {
}
}
}
private static void notBlankByAnnotate(Field field, Object obj) throws IllegalAccessException {
NotBlank notBlank = field.getAnnotation(NotBlank.class);
if (notBlank != null) {
if (String.class.equals(field.getType())) {
notBlank((String) field.get(obj), notBlank.message());
} else {
notNull(field.get(obj), notBlank.message());
}
}
}
private static void notNullByAnnotate(Field field, Object obj) throws IllegalAccessException {
NotNull notNull = field.getAnnotation(NotNull.class);
if (notNull != null) {
notNull(field.get(obj), notNull.message());
}
}
private static void notEmptyByAnnotate(Field field, Object obj) throws IllegalAccessException {
NotEmpty notEmpty = field.getAnnotation(NotEmpty.class);
if (notEmpty != null) {
Assert.notEmpty((Object[]) field.get(obj), notEmpty.message());
}
}
}