Browse Source

update AspectController

deason 6 years ago
parent
commit
df4ba7f32c

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

@@ -7,6 +7,7 @@
 
 package cn.com.qmth.examcloud.core.print.common;
 
+import cn.com.qmth.examcloud.core.print.common.utils.JsonMapper;
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import springfox.documentation.annotations.ApiIgnore;
 
@@ -84,4 +85,8 @@ public class Result<T> implements Serializable {
         return data;
     }
 
+    public String toJson() {
+        return new JsonMapper().toJson(this);
+    }
+
 }

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

@@ -11,39 +11,40 @@ 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.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 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);
+    public final static HttpStatus ERROR_CODE = HttpStatus.INTERNAL_SERVER_ERROR;
+    public final static String ERROR_CONTENT = "系统异常!";
 
     @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());
+    public ResponseEntity<String> handle(RuntimeException e) {
+        return doHandle(e);
     }
 
     @ResponseBody
     @ExceptionHandler(value = Exception.class)
-    public Result handle(Exception e) {
+    public ResponseEntity<String> handle(Exception e) {
+        return doHandle(e);
+    }
+
+    public static ResponseEntity<String> doHandle(Exception e) {
         if (e instanceof StatusException) {
             StatusException se = (StatusException) e;
             log.error(se.toJson());
-            return new Result(se.getCode(), se.getDesc());
+            return new ResponseEntity<>(se.toJson(), ERROR_CODE);
         }
         log.error(e.getMessage(), e);
-        return error(e.getMessage());
+        String errorJson = Result.error(ERROR_CONTENT).toJson();
+        return new ResponseEntity<>(errorJson, ERROR_CODE);
     }
 
 }

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

@@ -7,43 +7,25 @@
 
 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.http.ResponseEntity;
 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());
+    public ResponseEntity<String> handle(RuntimeException e) {
+        return AspectController.doHandle(e);
     }
 
     @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());
+    public ResponseEntity<String> handle(Exception e) {
+        return AspectController.doHandle(e);
     }
 
 }