|
|
@@ -1,5 +1,6 @@
|
|
|
package com.xintong.visualinspection.controller;
|
|
|
|
|
|
+import java.io.FileNotFoundException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@@ -9,6 +10,8 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.xintong.visualinspection.err.BusinessException;
|
|
|
+import com.xintong.visualinspection.err.ErrorCode;
|
|
|
|
|
|
/**
|
|
|
* 文件名:TestController
|
|
|
@@ -32,17 +35,57 @@ public class BaseController {
|
|
|
return JSON.toJSON(result).toString();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 返回前台结果结构体
|
|
|
+ * @return
|
|
|
+ * String
|
|
|
+ * @exception
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+ public String returnSuccessResult(String result_desc){
|
|
|
+ Map<String,Object> result = new HashMap<>();
|
|
|
+ result.put("result_code", 0);
|
|
|
+ result.put("result_desc", result_desc);
|
|
|
+ return JSON.toJSON(result).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回前台结果结构体
|
|
|
+ * @return
|
|
|
+ * String
|
|
|
+ * @exception
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+ public String returnSuccessResult(String result_desc, Object o){
|
|
|
+ Map<String,Object> result = new HashMap<>();
|
|
|
+ result.put("result_code", 0);
|
|
|
+ result.put("result_desc", result_desc);
|
|
|
+ result.put("result_data", o);
|
|
|
+ return JSON.toJSON(result).toString();
|
|
|
+ }
|
|
|
+
|
|
|
/** 基于@ExceptionHandler异常处理 */
|
|
|
@ExceptionHandler
|
|
|
- public String exp(int errcode, Exception ex) {
|
|
|
-// request.setAttribute("ex", ex);
|
|
|
- // 根据不同错误转向不同页面
|
|
|
- String err;
|
|
|
+ public String exp(HttpServletRequest request, Exception ex) {
|
|
|
+ // 根据不同错误提示不同的错误
|
|
|
+ ErrorCode code;
|
|
|
if(ex instanceof NullPointerException) {
|
|
|
- err = "空指针错误";
|
|
|
- } else {
|
|
|
- err = "好多错误";
|
|
|
- }
|
|
|
- return returnResult(errcode,err,null);
|
|
|
+ code = new ErrorCode(10001);
|
|
|
+ } else if (ex instanceof NumberFormatException) {
|
|
|
+ code = new ErrorCode(10002);
|
|
|
+ } else if (ex instanceof IndexOutOfBoundsException) {
|
|
|
+ code = new ErrorCode(10003);
|
|
|
+ } else if (ex instanceof ArithmeticException) {
|
|
|
+ code = new ErrorCode(10004);
|
|
|
+ } else if (ex instanceof FileNotFoundException) {
|
|
|
+ code = new ErrorCode(10005);
|
|
|
+ } else if (ex instanceof IllegalArgumentException) {
|
|
|
+ code = new ErrorCode(10006);
|
|
|
+ } else if (ex instanceof BusinessException) {
|
|
|
+ code = ((BusinessException) ex).getErrCode();
|
|
|
+ } else{
|
|
|
+ code = new ErrorCode(11000);
|
|
|
+ }
|
|
|
+ return returnResult(code.getCode(),code.getDesc(),null);
|
|
|
}
|
|
|
}
|