ApiExceptionResolver.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.xcgl.weixin.base;
  2. import com.xcgl.weixin.entity.WXAjaxJson;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.web.bind.annotation.ControllerAdvice;
  6. import org.springframework.web.bind.annotation.ExceptionHandler;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. /**
  9. * 全局Controller层异常处理类
  10. */
  11. @ControllerAdvice
  12. public class ApiExceptionResolver {
  13. private static final Logger LOG = LoggerFactory.getLogger(ApiExceptionResolver.class);
  14. // /**
  15. // * 处理所有不可知异常
  16. // *
  17. // * @param e 异常
  18. // * @return json结果
  19. // */
  20. // @ExceptionHandler(Exception.class)
  21. // @ResponseBody
  22. // public Result handleException(Exception e) {
  23. // // 打印异常堆栈信息
  24. // LOG.error(e.getMessage(), e);
  25. // return Result.error(ResultEnum.ERROR.getCode(), e.getMessage());
  26. // }
  27. /**
  28. * 处理所有业务异常
  29. *
  30. * @param e 业务异常
  31. * @return json结果
  32. */
  33. @ExceptionHandler(ApiException.class)
  34. @ResponseBody
  35. public WXAjaxJson handleOpdRuntimeException(ApiException e) {
  36. // 不打印异常堆栈信息
  37. LOG.error(e.getMessage());
  38. return WXAjaxJson.error(e.getCode(), e.getMessage());
  39. }
  40. }