review code.
This commit is contained in:
@@ -9,7 +9,9 @@ package com.orion.ops.framework.common.constant;
|
||||
*/
|
||||
public interface ErrorMessage {
|
||||
|
||||
String PARAM_MISSING = "{} 不能为空";
|
||||
String MISSING = "{} 不能为空";
|
||||
|
||||
String PARAM_MISSING = "参数不能为空";
|
||||
|
||||
String ID_MISSING = "id 不能为空";
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.orion.ops.framework.common.utils;
|
||||
|
||||
import com.orion.ops.framework.common.constant.ErrorMessage;
|
||||
import com.orion.spring.SpringHolder;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.Validator;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/7/18 11:23
|
||||
*/
|
||||
public class Valid extends com.orion.lang.utils.Valid {
|
||||
|
||||
private static final Validator VALIDATOR = SpringHolder.getBean(Validator.class);
|
||||
|
||||
/**
|
||||
* 验证枚举
|
||||
*
|
||||
* @param of of method
|
||||
* @param obj obj
|
||||
* @param <T> param
|
||||
* @param <E> enum
|
||||
* @return enum
|
||||
*/
|
||||
public static <T, E extends Enum<?>> E valid(Function<T, E> of, T obj) {
|
||||
return notNull(of.apply(obj), ErrorMessage.INVALID_PARAM);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证对象
|
||||
*
|
||||
* @param obj obj
|
||||
* @param groups groups
|
||||
* @param <T> T
|
||||
* @return obj
|
||||
*/
|
||||
public static <T> T valid(T obj, Class<?>... groups) {
|
||||
notNull(obj, ErrorMessage.PARAM_MISSING);
|
||||
// 验证对象
|
||||
Set<ConstraintViolation<T>> set = VALIDATOR.validate(obj, groups);
|
||||
if (!set.isEmpty()) {
|
||||
throw new ConstraintViolationException(set);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,9 +3,9 @@ package ${package.ServiceImpl};
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.orion.lang.define.wrapper.DataGrid;
|
||||
import com.orion.lang.utils.Valid;
|
||||
import com.orion.lang.utils.collect.Lists;
|
||||
import com.orion.ops.framework.common.constant.ErrorMessage;
|
||||
import com.orion.ops.framework.common.utils.Valid;
|
||||
#foreach($pkg in ${customFilePackages})
|
||||
import ${pkg}.*;
|
||||
#end
|
||||
|
||||
@@ -62,13 +62,12 @@ public class RedisUtils {
|
||||
* 获取并且设置 json
|
||||
*
|
||||
* @param define define
|
||||
* @param type type
|
||||
* @param processor processor
|
||||
* @param params params
|
||||
* @param <T> type
|
||||
*/
|
||||
public static <T> void processSetJson(CacheKeyDefine define, Class<T> type, Consumer<T> processor, Object... params) {
|
||||
processSetJson(define.format(params), define, type, processor);
|
||||
public static <T> void processSetJson(CacheKeyDefine define, Consumer<T> processor, Object... params) {
|
||||
processSetJson(define.format(params), define, processor);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,17 +75,17 @@ public class RedisUtils {
|
||||
*
|
||||
* @param key key
|
||||
* @param define define
|
||||
* @param type type
|
||||
* @param processor processor
|
||||
* @param <T> type
|
||||
*/
|
||||
public static <T> void processSetJson(String key, CacheKeyDefine define, Class<T> type, Consumer<T> processor) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> void processSetJson(String key, CacheKeyDefine define, Consumer<T> processor) {
|
||||
String value = redisTemplate.opsForValue().get(key);
|
||||
if (value == null) {
|
||||
return;
|
||||
}
|
||||
// 转换
|
||||
T cache = JSON.parseObject(value, type);
|
||||
T cache = (T) JSON.parseObject(value, define.getType());
|
||||
// 执行处理逻辑
|
||||
processor.accept(cache);
|
||||
// 重新设置
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.orion.lang.exception.argument.InvalidArgumentException;
|
||||
import com.orion.lang.exception.argument.RpcWrapperException;
|
||||
import com.orion.lang.utils.Exceptions;
|
||||
import com.orion.lang.utils.Strings;
|
||||
import com.orion.ops.framework.common.constant.Const;
|
||||
import com.orion.ops.framework.common.constant.ErrorCode;
|
||||
import com.orion.ops.framework.common.constant.ErrorMessage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -67,7 +68,7 @@ public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = MissingServletRequestParameterException.class)
|
||||
public HttpWrapper<?> missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException ex) {
|
||||
String message = Strings.format(ErrorMessage.PARAM_MISSING, ex.getParameterName());
|
||||
String message = Strings.format(ErrorMessage.MISSING, ex.getParameterName());
|
||||
log.error("missingServletRequestParameterExceptionHandler {}", message);
|
||||
return ErrorCode.BAD_REQUEST.wrapper().msg(message);
|
||||
}
|
||||
@@ -75,15 +76,15 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(value = BindException.class)
|
||||
public HttpWrapper<?> paramBindExceptionHandler(BindException ex) {
|
||||
FieldError error = Objects.requireNonNull(ex.getFieldError());
|
||||
String message = error.getField() + " " + error.getDefaultMessage();
|
||||
String message = error.getField() + Const.SPACE + error.getDefaultMessage();
|
||||
log.error("paramBindExceptionHandler {}", message);
|
||||
return ErrorCode.BAD_REQUEST.wrapper().msg(message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = ConstraintViolationException.class)
|
||||
public HttpWrapper<?> constraintViolationExceptionHandler(ConstraintViolationException ex) {
|
||||
ConstraintViolation<?> constraintViolation = ex.getConstraintViolations().iterator().next();
|
||||
String message = Objects.requireNonNull(constraintViolation).getMessage();
|
||||
ConstraintViolation<?> violation = Objects.requireNonNull(ex.getConstraintViolations().iterator().next());
|
||||
String message = violation.getPropertyPath().toString() + Const.SPACE + violation.getMessage();
|
||||
log.error("constraintViolationExceptionHandler {}", message);
|
||||
return ErrorCode.BAD_REQUEST.wrapper().msg(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user