xucaiqin 4 stundas atpakaļ
vecāks
revīzija
a5619050c7

+ 39 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/exception/ApiException.java

@@ -0,0 +1,39 @@
+package com.sckw.core.exception;
+
+/**
+ * 业务异常
+ *
+ * @author xcq
+ * @date 2023-02-22 13:54:05
+ **/
+public class ApiException extends RuntimeException {
+    private int code;
+    private String message;
+
+    public int getCode() {
+        return code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    @Override
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    public ApiException() {
+        this.code = 500;
+    }
+
+    public ApiException(String message) {
+        super(message);
+        this.message = message;
+        this.code = 500;
+    }
+}

+ 7 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/exception/GlobalSystemExceptionHandler.java

@@ -1,6 +1,7 @@
 package com.sckw.core.exception;
 
 import com.sckw.core.web.constant.HttpStatus;
+import com.sckw.core.web.response.ApiResult;
 import com.sckw.core.web.response.HttpResult;
 import jakarta.validation.ConstraintViolation;
 import jakarta.validation.ConstraintViolationException;
@@ -52,6 +53,12 @@ public class GlobalSystemExceptionHandler {
         log.error("业务异常,message={},param={}", ex.getMsg(), ex.getParam());
         return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, ex.getMessage());
     }
+    @ResponseBody
+    @ExceptionHandler(ApiException.class)
+    public ApiResult<String> apiExceptionHandler(ApiException ex) {
+        log.error("业务异常处理:{}", ex.getMessage());
+        return ApiResult.failed(ex.getMessage());
+    }
 
     /**
      * 前端自定义提示异常

+ 74 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/web/response/ApiResult.java

@@ -0,0 +1,74 @@
+package com.sckw.core.web.response;
+
+import com.sckw.core.web.constant.HttpStatus;
+import lombok.*;
+import lombok.experimental.Accessors;
+
+import java.io.Serial;
+import java.io.Serializable;
+
+
+@ToString
+@NoArgsConstructor
+@AllArgsConstructor
+@Accessors(chain = true)
+public class ApiResult<T> implements Serializable {
+
+    @Serial
+    private static final long serialVersionUID = 1L;
+
+    @Getter
+    @Setter
+    private String msg;
+    @Getter
+    @Setter
+    private Boolean status;
+    @Getter
+    @Setter
+    private String sign;
+    @Getter
+    @Setter
+    private T data;
+
+    public static <T> ApiResult<T> ok() {
+        return restResult(null, true, null);
+    }
+
+    public static <T> ApiResult<T> ok(T data) {
+        return restResult(data, true, null);
+    }
+
+    public static <T> ApiResult<T> ok(T data, String msg) {
+        return restResult(data, true, msg);
+    }
+
+    public static <T> ApiResult<T> failed() {
+        return restResult(null, false, null);
+    }
+
+    public static <T> ApiResult<T> failed(String msg) {
+        return restResult(null, false, msg);
+    }
+
+    public static <T> ApiResult<T> failed(T data) {
+        return restResult(data, false, null);
+    }
+
+    public static <T> ApiResult<T> failed(T data, String msg) {
+        return restResult(data, false, msg);
+    }
+
+    public static <T> ApiResult<T> failed(T data, String msg, int code) {
+        return restResult(data, false, msg);
+    }
+
+    private static <T> ApiResult<T> restResult(T data, Boolean status, String msg) {
+        ApiResult<T> apiResult = new ApiResult<>();
+        apiResult.setStatus(status);
+        apiResult.setData(data);
+        apiResult.setMsg(msg);
+        return apiResult;
+    }
+
+
+}