类 GlobalExceptionHandler

java.lang.Object
cn.daimaxia.framework.web.core.handler.GlobalExceptionHandler

@RestControllerAdvice public class GlobalExceptionHandler extends Object
全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号
作者:
ZhaoYang
  • 构造器详细资料

    • GlobalExceptionHandler

      public GlobalExceptionHandler()
  • 方法详细资料

    • allExceptionHandler

      public Result<?> allExceptionHandler(javax.servlet.http.HttpServletRequest request, Throwable ex)
    • missingServletRequestParameterExceptionHandler

      @ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class) public Result<?> missingServletRequestParameterExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException ex)
      处理 SpringMVC 请求参数缺失 例如说,接口上设置了 @RequestParam("xx") 参数,结果并未传递 xx 参数
    • methodArgumentTypeMismatchExceptionHandler

      @ExceptionHandler(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException.class) public Result<?> methodArgumentTypeMismatchExceptionHandler(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException ex)
      处理 SpringMVC 请求参数类型错误 例如说,接口上设置了 @RequestParam("xx") 参数为 Integer,结果传递 xx 参数类型为 String
    • methodArgumentNotValidExceptionExceptionHandler

      @ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public Result<?> methodArgumentNotValidExceptionExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException ex)
      处理 SpringMVC 参数校验不正确
    • bindExceptionHandler

      @ExceptionHandler(org.springframework.validation.BindException.class) public Result<?> bindExceptionHandler(org.springframework.validation.BindException ex)
      处理 SpringMVC 参数绑定不正确,本质上也是通过 Validator 校验
    • constraintViolationExceptionHandler

      @ExceptionHandler(javax.validation.ConstraintViolationException.class) public Result<?> constraintViolationExceptionHandler(javax.validation.ConstraintViolationException ex)
      处理 Validator 校验不通过产生的异常
    • validationException

      @ExceptionHandler(javax.validation.ValidationException.class) public Result<?> validationException(javax.validation.ValidationException ex)
      处理 Dubbo Consumer 本地参数校验时,抛出的 ValidationException 异常
    • noHandlerFoundExceptionHandler

      @ExceptionHandler(org.springframework.web.servlet.NoHandlerFoundException.class) public Result<?> noHandlerFoundExceptionHandler(org.springframework.web.servlet.NoHandlerFoundException ex)
      处理 SpringMVC 请求地址不存在 注意,它需要设置如下两个配置项: 1. spring.mvc.throw-exception-if-no-handler-found 为 true 2. spring.mvc.static-path-pattern 为 /statics/**
    • httpRequestMethodNotSupportedExceptionHandler

      @ExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException.class) public Result<?> httpRequestMethodNotSupportedExceptionHandler(org.springframework.web.HttpRequestMethodNotSupportedException ex)
      处理 SpringMVC 请求方法不正确 例如说,A 接口的方法为 GET 方式,结果请求方法为 POST 方式,导致不匹配
    • serviceExceptionHandler

      @ExceptionHandler(cn.daimaxia.framework.common.exception.ServiceException.class) public Result<?> serviceExceptionHandler(ServiceException ex)
      处理业务异常 ServiceException 例如说,商品库存不足,用户手机号已存在。
    • defaultExceptionHandler

      @ExceptionHandler(java.lang.Exception.class) public Result<?> defaultExceptionHandler(javax.servlet.http.HttpServletRequest req, Throwable ex)
      处理系统异常,兜底处理所有的一切