소스 검색

修改springboot的自动异常错误

houweiyu 4 년 전
부모
커밋
50af2855a2
1개의 변경된 파일32개의 추가작업 그리고 0개의 파일을 삭제
  1. 32 0
      src/main/java/fcb/project/manager/base/exception/ControllerHanderException.java

+ 32 - 0
src/main/java/fcb/project/manager/base/exception/ControllerHanderException.java

@@ -0,0 +1,32 @@
+package fcb.project.manager.base.exception;
+
+import fdage.back.sdk.base.entity.Result;
+import fdage.back.sdk.base.exception.CommonBaseException;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+/**
+ * @author abnerhou
+ * @date 2020/9/27 19:09
+ * @desciption
+ */
+@ControllerAdvice
+public class ControllerHanderException {
+
+    @ExceptionHandler(CommonBaseException.class)
+    @ResponseBody
+    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+    //在这个方法里定义我们需要返回的格式
+//    public Map<String, Object> handleUserNotExistException(CommonBaseException ex){
+    public Result handleUserNotExistException(CommonBaseException e){
+       /* Map<String, Object> result = new HashMap<>();
+        result.put("code", ex.getCode());
+        result.put("msg", ex.getMsg());
+        return result;*/
+        return Result.failure(null == e.getCode() ? Result.CODE_FAILURE : e.getCode(), e.getMsg());
+    }
+
+}