xiatian 15 ساعت پیش
والد
کامیت
37dd44b849

+ 38 - 16
stmms-web/src/main/java/cn/com/qmth/stmms/api/controller/BaseApiController.java

@@ -18,19 +18,23 @@ import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
 
 import cn.com.qmth.stmms.admin.utils.SessionExamUtils;
 import cn.com.qmth.stmms.api.exception.ApiException;
 import cn.com.qmth.stmms.biz.exam.bean.ResultMessage;
 import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
 import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
+import cn.com.qmth.stmms.biz.exception.StatusException;
 import cn.com.qmth.stmms.biz.school.model.School;
 import cn.com.qmth.stmms.biz.school.service.SchoolService;
+import cn.com.qmth.stmms.common.StatusResponse;
 import cn.com.qmth.stmms.common.controller.BaseController;
 import cn.com.qmth.stmms.common.domain.ApiUser;
-import cn.com.qmth.stmms.common.domain.WebUser;
 import cn.com.qmth.stmms.common.utils.RequestUtils;
 import net.sf.json.JSONObject;
 
@@ -38,28 +42,46 @@ public class BaseApiController extends BaseController {
 
     protected static final Logger log = LoggerFactory.getLogger(BaseApiController.class);
 
-    private static final String ERROR_MESSAGE_HEADER_KEY = "error-info";
-
     @Autowired
     private SchoolService schoolService;
 
     @Autowired
     private ExamSubjectService subjectService;
 
-    @ExceptionHandler
-    public void exception(HttpServletResponse response, Exception ex) {
-        log.error("api execute error", ex);
-        response.addHeader(ERROR_MESSAGE_HEADER_KEY, StringUtils.trimToEmpty(ex.getMessage()));
-        try {
-            if (ex instanceof ApiException) {
-                ApiException e = (ApiException) ex;
-                response.sendError(e.getCode());
-            } else {
-                response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value());
-            }
-        } catch (Exception e) {
-            log.error("api response senderror", e);
+    @ResponseBody
+    @ExceptionHandler(Exception.class)
+    public ResponseEntity<StatusResponse> handleException(Exception e, HttpServletRequest request) {
+        StatusResponse body = new StatusResponse("500", "系统异常");
+        return asResult(e, body, request);
+    }
+
+    @ResponseBody
+    @ExceptionHandler(RuntimeException.class)
+    public ResponseEntity<StatusResponse> handleException(RuntimeException e, HttpServletRequest request) {
+        StatusResponse body = null;
+
+        if (e instanceof StatusException) {
+            StatusException se = (StatusException) e;
+            body = new StatusResponse(se.getCode(), se.getDesc());
+        } else if (e instanceof com.qmth.boot.core.exception.StatusException) {
+            com.qmth.boot.core.exception.StatusException se = (com.qmth.boot.core.exception.StatusException) e;
+            body = new StatusResponse(se.getCode() + "", se.getMessage());
+        } else {
+            body = new StatusResponse("500", "系统异常");
         }
+
+        return asResult(e, body, request);
+    }
+
+    private ResponseEntity<StatusResponse> asResult(Throwable err, StatusResponse body, HttpServletRequest request) {
+
+        HttpStatus httpStatus = HttpStatus.SERVICE_UNAVAILABLE;
+
+        log.error(err.getMessage(), err);
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.add("Content-Type", "application/json;charset=utf-8");
+        return new ResponseEntity<>(body, headers, httpStatus);
     }
 
     protected void exportFile(String fileName, File file, HttpServletResponse response) {

+ 7 - 0
stmms-web/src/main/java/cn/com/qmth/stmms/common/JsonSerializable.java

@@ -0,0 +1,7 @@
+package cn.com.qmth.stmms.common;
+
+import java.io.Serializable;
+
+public interface JsonSerializable extends Serializable {
+
+}

+ 50 - 0
stmms-web/src/main/java/cn/com/qmth/stmms/common/StatusResponse.java

@@ -0,0 +1,50 @@
+package cn.com.qmth.stmms.common;
+
+import io.swagger.annotations.ApiModelProperty;
+
+public class StatusResponse implements JsonSerializable {
+
+    private static final long serialVersionUID = 8393074113722405560L;
+
+    @ApiModelProperty(value = "响应编码", example = "xxx", required = true)
+    private String code;
+
+    @ApiModelProperty(value = "响应描述", example = "具体描述信息", required = true)
+    private String desc;
+
+    /**
+     * 构造函数
+     */
+    public StatusResponse() {
+        super();
+    }
+
+    /**
+     * 构造函数
+     *
+     * @param code
+     * @param desc
+     */
+    public StatusResponse(String code, String desc) {
+        super();
+        this.code = code;
+        this.desc = desc;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+
+    public void setDesc(String desc) {
+        this.desc = desc;
+    }
+
+}