|
@@ -0,0 +1,49 @@
|
|
|
|
+package com.free.frame;
|
|
|
|
+
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.PrintStream;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 自定义异常拦截
|
|
|
|
+ */
|
|
|
|
+@ControllerAdvice
|
|
|
|
+public class GlobalExceptionController {
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(GlobalExceptionController.class);
|
|
|
|
+ @ExceptionHandler(value = CustomizationException.class)
|
|
|
|
+ @ResponseBody
|
|
|
|
+ public ResponseFormat exception(HttpServletRequest req, CustomizationException ce) {
|
|
|
|
+ log.error("==========自定义异常捕获==========");
|
|
|
|
+ log.error(getExceptionInfo(ce));
|
|
|
|
+ return ResponseFormat.customizationError(ce);
|
|
|
|
+ }
|
|
|
|
+ @ExceptionHandler(value = Exception.class)
|
|
|
|
+ @ResponseBody
|
|
|
|
+ public ResponseFormat exceptionHandler(HttpServletRequest req, Exception e) {
|
|
|
|
+ log.error("==========全局异常捕获==========");
|
|
|
|
+ log.error(getExceptionInfo(e));
|
|
|
|
+ return ResponseFormat.error();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static String getExceptionInfo(Exception ex) {
|
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
+ PrintStream printStream = new PrintStream(out);
|
|
|
|
+ ex.printStackTrace(printStream);
|
|
|
|
+ String rs = new String(out.toByteArray());
|
|
|
|
+ try {
|
|
|
|
+ printStream.close();
|
|
|
|
+ out.close();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return rs;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|