ExceptionHandlerAdvice.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*CopyRright (c)2014: <www.usoftchina.com>
  2. */
  3. package com.uas.report.exception;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.http.HttpHeaders;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.ui.ModelMap;
  10. import org.springframework.web.bind.annotation.ControllerAdvice;
  11. import org.springframework.web.bind.annotation.ExceptionHandler;
  12. import com.uas.report.util.ExceptionUtils;
  13. /**
  14. * 基于Application的异常处理,以AOP的形式注册到SpringMVC的处理链
  15. *
  16. * @author sunyj
  17. * @since 2017年7月13日 上午10:28:37
  18. */
  19. @ControllerAdvice
  20. public class ExceptionHandlerAdvice {
  21. private static Logger logger = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
  22. /**
  23. * 处理已捕获异常,明确传达给客户端错误码、错误信息
  24. *
  25. * @param e
  26. * @return
  27. */
  28. @ExceptionHandler(Throwable.class)
  29. public ResponseEntity<ModelMap> handleError(Throwable e) {
  30. logger.error("", e);
  31. ModelMap map = new ModelMap();
  32. map.put("success", false);
  33. map.put("message", e.getMessage());
  34. map.put("detailedMessage", ExceptionUtils.getDetailedMessage(e));
  35. HttpHeaders headers = new HttpHeaders();
  36. headers.add("Content-Type", "application/json; charset=utf-8");
  37. return new ResponseEntity<ModelMap>(map, headers, HttpStatus.INTERNAL_SERVER_ERROR);
  38. }
  39. }