瀏覽代碼

修改通用返回

lyhzzz 3 年之前
父節點
當前提交
f1a17b5991

+ 7 - 4
4dkankan-common/src/main/java/com/fdkankan/common/exception/GlobalExceptionHandler.java

@@ -4,6 +4,7 @@ import cn.hutool.core.exceptions.ExceptionUtil;
 import com.fdkankan.common.constant.ServerCode;
 import com.fdkankan.common.response.ResultData;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
@@ -17,14 +18,16 @@ import javax.servlet.http.HttpServletRequest;
 @Slf4j
 public class GlobalExceptionHandler {
 
+    @Autowired
+    private ResultData resultData;
     /**
      * 处理未知异常
      */
     @ResponseBody
     @ExceptionHandler(value = Exception.class)
-    public ResultData exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
+    public String exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
         log.error("服务错误:", e);
-        return ResultData.fail(ServerCode.SYSTEM_ERROR.code(), ExceptionUtil.stacktraceToString(e, 3000));
+        return resultData.error(ServerCode.SYSTEM_ERROR.code(), ExceptionUtil.stacktraceToString(e, 3000));
     }
 
     /**
@@ -32,8 +35,8 @@ public class GlobalExceptionHandler {
      */
     @ResponseBody
     @ExceptionHandler(value = BusinessException.class)
-    public ResultData businessExceptionHandler(HttpServletRequest httpServletRequest, BusinessException e) {
+    public String businessExceptionHandler(HttpServletRequest httpServletRequest, BusinessException e) {
         log.info("业务异常-----code:" + e.getCode() + "msg:" + e.getMsg());
-        return ResultData.fail(e.getCode(), e.getMsg());
+        return resultData.error(e.getCode(), e.getMsg());
     }
 }

+ 28 - 12
4dkankan-common/src/main/java/com/fdkankan/common/response/ResultData.java

@@ -1,18 +1,23 @@
 package com.fdkankan.common.response;
 
+import com.alibaba.fastjson.JSON;
+import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import org.springframework.stereotype.Component;
 
 import java.io.Serializable;
 import java.util.Calendar;
 
+@Component
 @Data
 @NoArgsConstructor
-public class ResultData<T> implements Serializable {
+@AllArgsConstructor
+public class ResultData implements Serializable {
     /**
      * 状态码
      */
-    private int status;
+    private int code;
     /**
      * 响应信息
      */
@@ -20,23 +25,34 @@ public class ResultData<T> implements Serializable {
     /**
      * 后端返回结果
      */
-    private T data;
-    /**
-     * 后端响应状态
-     */
-    private boolean success;
+    private Object data;
     /**
      * 响应时间戳
      */
     private long timestamp = Calendar.getInstance().getTimeInMillis();
 
-    public ResultData(int status, String message){
-        this.status = status;
-        this.message = message;
+    public String ok() {
+        return this.ok("");
+    }
+    public String ok(Object data) {
+        return this.ok("", data);
+    }
+    public String ok(String msg, Object data) {
+        return this.base(200, msg, data);
     }
 
-    public static <T> ResultData<T> fail(int status, String message){
-        return  new ResultData<>(status, message);
+    public String error(int code, String msg) {
+        return this.error(code, msg, "");
+    }
+    public String error(int code, String msg, Object data) {
+        return this.base(code, msg, data);
     }
 
+    private String base(int code, String msg, Object data) {
+        ResultData rd = new ResultData();
+        rd.setCode(code);
+        rd.setMessage(msg);
+        rd.setData(data);
+        return JSON.toJSONString(rd);
+    }
 }