Browse Source

添加基础工具类, 修改业务异常的占位符,添加校验类

Gaokun Wang 1 tháng trước cách đây
mục cha
commit
e214cf3e10

+ 2 - 3
eco-common/com-core/src/main/java/org/eco/vip/orm/exception/BusinessException.java

@@ -10,8 +10,7 @@ import cn.hutool.core.util.StrUtil;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import org.eco.vip.orm.exception.enums.BusinessErrorCode;
-
-import java.text.MessageFormat;
+import org.eco.vip.orm.utils.StringUtil;
 
 /**
  * @description 业务逻辑异常
@@ -75,7 +74,7 @@ public final class BusinessException extends RuntimeException {
     public String getMessage() {
         if (StrUtil.isNotBlank(message)) {
             if (msgParams != null && msgParams.length > 0) {
-                return MessageFormat.format(message, msgParams);
+                return StringUtil.format(message, msgParams);
             }
         }
         // 如果不传参数,直接返回

+ 118 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/utils/JsonUtil.java

@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.utils;
+
+
+import cn.hutool.core.lang.Dict;
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.json.JSONUtil;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.exc.MismatchedInputException;
+import io.micrometer.common.util.StringUtils;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @description JsonUtil
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 16:27
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class JsonUtil extends JSONUtil {
+    private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class);
+
+    public static ObjectMapper getObjectMapper() {
+        return OBJECT_MAPPER;
+    }
+
+    public static String toJsonString(Object object) {
+        if (ObjectUtil.isNull(object)) {
+            return null;
+        }
+        try {
+            return OBJECT_MAPPER.writeValueAsString(object);
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> T parseObject(String text, Class<T> clazz) {
+        if (StringUtils.isEmpty(text)) {
+            return null;
+        }
+        try {
+            return OBJECT_MAPPER.readValue(text, clazz);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
+        if (ArrayUtil.isEmpty(bytes)) {
+            return null;
+        }
+        try {
+            return OBJECT_MAPPER.readValue(bytes, clazz);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> T parseObject(String text, TypeReference<T> typeReference) {
+        if (StringUtils.isBlank(text)) {
+            return null;
+        }
+        try {
+            return OBJECT_MAPPER.readValue(text, typeReference);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static Dict parseMap(String text) {
+        if (StringUtils.isBlank(text)) {
+            return null;
+        }
+        try {
+            return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class));
+        } catch (MismatchedInputException e) {
+            // 类型不匹配说明不是json
+            return null;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static List<Dict> parseArrayMap(String text) {
+        if (StringUtils.isBlank(text)) {
+            return null;
+        }
+        try {
+            return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static <T> List<T> parseArray(String text, Class<T> clazz) {
+        if (StringUtils.isEmpty(text)) {
+            return new ArrayList<>();
+        }
+        try {
+            return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

+ 18 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/utils/SpringUtils.java

@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.utils;
+
+
+import cn.hutool.extra.spring.SpringUtil;
+
+/**
+ * @description SpringUtils
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 16:22
+ */
+public final class SpringUtils extends SpringUtil {
+}

+ 20 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/utils/StringUtil.java

@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.utils;
+
+
+import cn.hutool.core.util.StrUtil;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * @description StrUtil
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 16:51
+ */
+@Slf4j
+public class StringUtil extends StrUtil {
+}

+ 37 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/utils/ValidatorUtils.java

@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.utils;
+
+
+import jakarta.validation.ConstraintViolation;
+import jakarta.validation.Validator;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.eco.vip.orm.exception.BusinessException;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * @description ValidatorUtils
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 16:21
+ */
+@Slf4j
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class ValidatorUtils {
+    private static final Validator VALID = SpringUtils.getBean(Validator.class);
+
+    public static <T> void validate(T object, Class<?>... groups) {
+        Set<ConstraintViolation<T>> validate = VALID.validate(object, groups);
+        if (!validate.isEmpty()) {
+            String msg = validate.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(","));
+            throw new BusinessException("参数校验异常: {}", msg);
+        }
+    }
+}

+ 3 - 5
eco-common/com-web/src/main/java/org/eco/vip/web/config/core/handler/GlobalExceptionHandler.java

@@ -22,7 +22,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 
 import java.util.List;
-import java.util.Objects;
 
 /**
  * @description GlobalExceptionHandler 全局异常处理器
@@ -38,7 +37,6 @@ public class GlobalExceptionHandler {
         if (throwable instanceof MethodArgumentNotValidException) {
             return handleMethodArgumentNotValidException((MethodArgumentNotValidException) throwable);
         }
-
         return defaultExceptionHandler(request, throwable);
     }
 
@@ -46,7 +44,7 @@ public class GlobalExceptionHandler {
      * 业务异常
      */
     @ExceptionHandler(BusinessException.class)
-    public CommonResult<Void> handleServiceException(BusinessException e, HttpServletRequest request) {
+    public CommonResult<Void> handleServiceException(BusinessException e) {
         log.error(e.getMessage());
         Integer code = e.getCode();
         return ObjectUtil.isNotNull(code) ? CommonResult.fail(code, e.getMessage()) : CommonResult.fail(e.getMessage());
@@ -62,10 +60,10 @@ public class GlobalExceptionHandler {
         FieldError fieldError = exception.getBindingResult().getFieldError();
         if (ObjectUtil.isNull(fieldError)) {
             List<ObjectError> allErrors = exception.getBindingResult().getAllErrors();
-            if(CollUtil.isNotEmpty(allErrors)) {
+            if (CollUtil.isNotEmpty(allErrors)) {
                 errorMessage = allErrors.getFirst().getDefaultMessage();
             }
-        }else  {
+        } else {
             errorMessage = fieldError.getDefaultMessage();
         }
         if (StrUtil.isEmpty(errorMessage)) {