top.doudou.core.util.ParamUtil Maven / Gradle / Ivy
package top.doudou.core.util;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import top.doudou.core.exception.CustomException;
import javax.validation.constraints.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static top.doudou.core.constant.RegexConstant.EMAIL_REGEX;
/**
* 验证参数
*
* @author 傻男人<[email protected]>
* @date 2020-04-03
*/
@Slf4j
public class ParamUtil {
/**
* 检查入参是否有错误
* @param result
*/
public static void getErrorMsg(BindingResult result){
if (result.hasErrors()) {
throw new CustomException(result.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining(",")));
}
}
/**
* 检查入参是否有错误
* @param result
*/
public static void getFiledErrorMsg(BindingResult result){
if (result.hasErrors()) {
Map collect = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
throw new CustomException(new Gson().toJson(collect));
}
}
/**
* 参数验证
*
* @param t
* @param
*/
public static void checkParam(T t) {
if (t == null) {
throw new CustomException("参数不能为空");
}
Class> clazz = t.getClass();
Field[] fields = clazz.getDeclaredFields();
try {
for (Field field : fields) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers)) {
continue;
}
Annotation[] annotations = field.getDeclaredAnnotations();
if (annotations.length == 0) {
continue;
}
if (!field.isAnnotationPresent(NotBlank.class) && !field.isAnnotationPresent(NotNull.class) && !field.isAnnotationPresent(Pattern.class) && !field.isAnnotationPresent(Email.class)) {
continue;
}
Object value = FieldUtils.getFieldValue(field,t);
NotBlank notblank = field.getDeclaredAnnotation(NotBlank.class);
if (notblank != null) {
if( null == value || StringUtils.isEmpty(value.toString())){
throw new CustomException(notblank.message());
}
}
NotEmpty notEmpty = field.getDeclaredAnnotation(NotEmpty.class);
if (notEmpty != null) {
if(null == value){
throw new CustomException(notEmpty.message());
}
boolean stringEmpty = value instanceof String && StringUtils.isEmpty(value.toString());
boolean ListEmpty = value instanceof List && CollectionUtils.isEmpty((List) value);
boolean mapEmpty = value instanceof Map && ((Map)value).size() == 0;
if (stringEmpty || ListEmpty || mapEmpty) {
throw new CustomException(notEmpty.message());
}
}
NotNull notNull = field.getDeclaredAnnotation(NotNull.class);
if (notNull != null && null == value) {
throw new CustomException(notNull.message());
}
Pattern pattern = field.getDeclaredAnnotation(Pattern.class);
if (pattern != null) {
if (value == null) {
throw new CustomException(pattern.message());
}
boolean matches = value.toString().matches(pattern.regexp());
if (!matches) {
throw new CustomException(pattern.message());
}
}
Email email = field.getDeclaredAnnotation(Email.class);
if (email != null) {
if (value == null) {
throw new CustomException(email.message());
}
boolean matches = value.toString().matches(EMAIL_REGEX);
if (!matches) {
throw new CustomException("邮箱格式不正确");
}
}
}
} catch (Exception e) {
throw new CustomException(e.getMessage());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy