Browse Source

update aspect response result.

deason 6 years ago
parent
commit
63f97f5a0d

+ 5 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/Constants.java

@@ -15,6 +15,11 @@ package cn.com.qmth.examcloud.core.print.common;
  */
 public interface Constants {
 
+    /**
+     * 操作成功
+     */
+    String PRT_CODE_200 = "PRT-000200";
+
     /**
      * 系统错误
      */

+ 87 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/Result.java

@@ -0,0 +1,87 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-10-25 11:18:18.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.core.print.common;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import springfox.documentation.annotations.ApiIgnore;
+
+import java.io.Serializable;
+
+import static cn.com.qmth.examcloud.core.print.common.Constants.*;
+
+/**
+ * 响应结果类
+ */
+public class Result<T> implements Serializable {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 状态码
+     */
+    private String code;
+    /**
+     * 描述信息
+     */
+    private String desc;
+    /**
+     * 结果数据
+     */
+    private T data;
+
+    public Result(String code, String desc, T data) {
+        this.code = code;
+        this.desc = desc;
+        this.data = data;
+    }
+
+    public Result(String code, String desc) {
+        this.code = code;
+        this.desc = desc;
+    }
+
+    public static Result success() {
+        return success(null);
+    }
+
+    public static <T> Result success(T data) {
+        return new Result(PRT_CODE_200, "操作成功!", data);
+    }
+
+    public static Result error() {
+        return error("操作失败!");
+    }
+
+    public static Result error(String desc) {
+        return new Result(PRT_CODE_500, desc);
+    }
+
+    public static Result noAuth() {
+        return new Result(PRT_CODE_403, "无效认证信息,请先登录!");
+    }
+
+    @JsonIgnore
+    @ApiIgnore
+    public boolean isSuccess() {
+        if (PRT_CODE_200.equals(getCode())) {
+            return true;
+        }
+        return false;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public T getData() {
+        return data;
+    }
+
+}

+ 49 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/aspect/AspectController.java

@@ -0,0 +1,49 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-10-25 13:52:52.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.core.print.common.aspect;
+
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
+import cn.com.qmth.examcloud.core.print.common.Result;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import static cn.com.qmth.examcloud.core.print.common.Result.error;
+
+@ControllerAdvice(annotations = Controller.class)
+public class AspectController {
+    private final static Logger log = LoggerFactory.getLogger(AspectController.class);
+
+    @ResponseBody
+    @ExceptionHandler(value = RuntimeException.class)
+    public Result handle(RuntimeException e) {
+        if (e instanceof StatusException) {
+            StatusException se = (StatusException) e;
+            log.error(se.toJson());
+            return new Result(se.getCode(), se.getDesc());
+        }
+        log.error(e.getMessage(), e);
+        return error(e.getMessage());
+    }
+
+    @ResponseBody
+    @ExceptionHandler(value = Exception.class)
+    public Result handle(Exception e) {
+        if (e instanceof StatusException) {
+            StatusException se = (StatusException) e;
+            log.error(se.toJson());
+            return new Result(se.getCode(), se.getDesc());
+        }
+        log.error(e.getMessage(), e);
+        return error(e.getMessage());
+    }
+
+}

+ 49 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/aspect/AspectRestController.java

@@ -0,0 +1,49 @@
+/*
+ * *************************************************
+ * Copyright (c) 2018 QMTH. All Rights Reserved.
+ * Created by Deason on 2018-10-25 13:52:55.
+ * *************************************************
+ */
+
+package cn.com.qmth.examcloud.core.print.common.aspect;
+
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
+import cn.com.qmth.examcloud.core.print.common.Result;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import static cn.com.qmth.examcloud.core.print.common.Result.error;
+
+@RestControllerAdvice(annotations = RestController.class)
+public class AspectRestController {
+    private final static Logger log = LoggerFactory.getLogger(AspectRestController.class);
+
+    @ResponseBody
+    @ExceptionHandler(value = RuntimeException.class)
+    public Result handle(RuntimeException e) {
+        if (e instanceof StatusException) {
+            StatusException se = (StatusException) e;
+            log.error(se.toJson());
+            return new Result(se.getCode(), se.getDesc());
+        }
+        log.error(e.getMessage(), e);
+        return error(e.getMessage());
+    }
+
+    @ResponseBody
+    @ExceptionHandler(value = Exception.class)
+    public Result handle(Exception e) {
+        if (e instanceof StatusException) {
+            StatusException se = (StatusException) e;
+            log.error(se.toJson());
+            return new Result(se.getCode(), se.getDesc());
+        }
+        log.error(e.getMessage(), e);
+        return error(e.getMessage());
+    }
+
+}