GlobalExceptionHandler.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.idea.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.RestControllerAdvice;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. /**
  9. * 全局异常处理
  10. */
  11. @RestControllerAdvice
  12. public class GlobalExceptionHandler {
  13. private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
  14. // @ExceptionHandler(Exception.class)
  15. // @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
  16. // public Map<String,Object> handlerBusinessException(Exception exception) {
  17. //// LOGGER.error("exception happened at {}", sunException.getMsg());
  18. // System.out.println("全局异常处理");
  19. // exception.printStackTrace();
  20. // Map<String, Object> map = new HashMap<>();
  21. // map.put("code","500");
  22. // map.put("message",exception.getMessage());
  23. // return map;
  24. //
  25. // }
  26. @ExceptionHandler(Exception.class)
  27. public Map<String,Object> handlerBusinessException(Exception exception) {
  28. // LOGGER.error("exception happened at {}", sunException.getMsg());
  29. // if (exception instanceof LoginCheckException){
  30. // System.out.println("全局登录异常处理");
  31. // exception.printStackTrace();
  32. // Map<String, Object> map = new HashMap<>();
  33. // map.put("code","501");
  34. // map.put("message",exception.toString());
  35. // map.put("data",null);
  36. // return map;
  37. // }
  38. System.out.println("全局异常处理");
  39. System.out.println(exception);
  40. LOGGER.error(exception.getMessage(),exception);
  41. exception.printStackTrace();
  42. Map<String, Object> map = new HashMap<>();
  43. map.put("code","500");
  44. map.put("key","500");
  45. map.put("msg",exception.getMessage());
  46. map.put("data",exception.toString());
  47. return map;
  48. }
  49. }