ExceptionAdvice.java 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. package com.koobietech.eas.config;
  2. import com.koobietech.eas.common.result.JsonResult;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.web.bind.annotation.ExceptionHandler;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import org.springframework.web.bind.annotation.ResponseStatus;
  9. import org.springframework.web.bind.annotation.RestControllerAdvice;
  10. @RestControllerAdvice
  11. public class ExceptionAdvice {
  12. private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);
  13. @ResponseBody
  14. @ResponseStatus(HttpStatus.OK)
  15. @ExceptionHandler(Exception.class)
  16. public JsonResult exceptionHandler(Exception e){
  17. StackTraceElement[] stackTrace = e.getStackTrace();
  18. e.printStackTrace();
  19. logger.error("运行异常提醒:");
  20. logger.error(e.getMessage());
  21. logger.error("异常堆栈信息:");
  22. for (StackTraceElement element : stackTrace) {
  23. logger.error(element.toString());
  24. }
  25. logger.error("-------------");
  26. return JsonResult.fail(e.getMessage());
  27. }
  28. }