|
@@ -0,0 +1,58 @@
|
|
|
|
+package com.idea.util;
|
|
|
|
+
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
+
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 全局异常处理
|
|
|
|
+ */
|
|
|
|
+@RestControllerAdvice
|
|
|
|
+public class GlobalExceptionHandler {
|
|
|
|
+
|
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
|
+
|
|
|
|
+// @ExceptionHandler(Exception.class)
|
|
|
|
+// @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
+// public Map<String,Object> handlerBusinessException(Exception exception) {
|
|
|
|
+//// LOGGER.error("exception happened at {}", sunException.getMsg());
|
|
|
|
+// System.out.println("全局异常处理");
|
|
|
|
+// exception.printStackTrace();
|
|
|
|
+// Map<String, Object> map = new HashMap<>();
|
|
|
|
+// map.put("code","500");
|
|
|
|
+// map.put("message",exception.getMessage());
|
|
|
|
+// return map;
|
|
|
|
+//
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
|
+ public Map<String,Object> handlerBusinessException(Exception exception) {
|
|
|
|
+// LOGGER.error("exception happened at {}", sunException.getMsg());
|
|
|
|
+// if (exception instanceof LoginCheckException){
|
|
|
|
+// System.out.println("全局登录异常处理");
|
|
|
|
+// exception.printStackTrace();
|
|
|
|
+// Map<String, Object> map = new HashMap<>();
|
|
|
|
+// map.put("code","501");
|
|
|
|
+// map.put("message",exception.toString());
|
|
|
|
+// map.put("data",null);
|
|
|
|
+// return map;
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ System.out.println("全局异常处理");
|
|
|
|
+ System.out.println(exception);
|
|
|
|
+ LOGGER.error(exception.getMessage(),exception);
|
|
|
|
+ exception.printStackTrace();
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+ map.put("code","500");
|
|
|
|
+ map.put("key","500");
|
|
|
|
+ map.put("msg",exception.getMessage());
|
|
|
|
+ map.put("data",exception.toString());
|
|
|
|
+ return map;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|