ExceptionController.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. package com.zy.bms.common.controller;
  2. import com.zy.bms.common.ServerResponse;
  3. import com.zy.bms.common.exception.ApiRuntimeException;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.RestControllerAdvice;
  6. import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
  7. /**
  8. * 全局异常拦截
  9. *
  10. * @author chenyi
  11. * Create on 2019/10/21
  12. */
  13. @RestControllerAdvice
  14. public class ExceptionController {
  15. /**
  16. * 对参数类型不匹配异常做统一拦截处理
  17. */
  18. @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
  19. public ServerResponse MethodArgumentTypeMismatchExceptionHandler() {
  20. return ServerResponse.createByErrorMsg("参数类型错误");
  21. }
  22. /**
  23. * 对运行时异常拦截
  24. */
  25. @ExceptionHandler(value = ApiRuntimeException.class)
  26. public ServerResponse ApiRuntimeExceptionHandler(ApiRuntimeException apiRuntimeException) {
  27. return ServerResponse.createByErrorMsg(apiRuntimeException.getMsg());
  28. }
  29. }