Java架构之路- 统一异常处理

在web应用中请求处理时,出现异常是非常常见的。所以当应用出现各类异常时,进行异常的捕获和处理非常重要的,本文重点讲解一下统一异常和数据校验

利用@ControllerAdvice和@ExceptionHandler定义一个统一异常处理类
@ControllerAdvice:控制器增强,使@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法。
@ExceptionHandler:异常处理器,此注解的作用是当出现其定义的异常时进行处理的方法

通用业务异常
/**
 * 通用业务异常
 *
 */
@NoArgsConstructor
public class BizException extends RuntimeException {

    public BizException(String message) {
        super(message);
    }

    public BizException(String message, Throwable cause) {
        super(message, cause);
    }

    public BizException(Throwable cause) {
        super(cause);
    }
}
提示异常
/**
 * 仅用于提示的异常,该异常不会导致输出堆栈
 *
 */
@NoArgsConstructor
public class TipException extends RuntimeException {

    public TipException(String message) {
        super(message);
    }

}
全局异常
/**
 * 全局异常拦截,主要拦截验证信息
 *
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 参数校验失败异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public BaseResponse handleBindException(BindException e) {
        return buildErrorResponse(e, e.getBindingResult());
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public BaseResponse handleBindException(MethodArgumentNotValidException e) {
        return buildErrorResponse(e, e.getBindingResult());
    }

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public BaseResponse handleHttpMethodException(HttpRequestMethodNotSupportedException e) {
        String msg = String.format("不支持 [%s] 请求", e.getMethod());
        return Response.fail(msg).build();
    }

    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    @ResponseBody
    public BaseResponse handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        String msg = String.format("缺少 %s 参数", e.getParameterName());
        return Response.fail(msg).build();
    }

    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public BaseResponse handleBizException(BizException e) {
        return Response.fail(e.getMessage()).build();
    }

    @ExceptionHandler(value = TipException.class)
    @ResponseBody
    public BaseResponse handleTipException(TipException e) {
        return Response.fail(e.getMessage()).build();
    }

    @ExceptionHandler(value = MultipartException.class)
    @ResponseBody
    public BaseResponse handleMultipartException(MultipartException e) {
        return Response.fail(e.getCause().getMessage()).build();
    }

    @ExceptionHandler(value = MissingServletRequestPartException.class)
    @ResponseBody
    public BaseResponse handleMissingServletRequestPartException(MissingServletRequestPartException e) {
        return Response.fail("请上传文件").build();
    }

    private BaseResponse buildErrorResponse(Exception e, BindingResult bindingResult) {
        if (null != bindingResult && bindingResult.hasFieldErrors()) {
            return Response.fail(bindingResult.getFieldError().getDefaultMessage()).build();
        }
        log.warn("参数校验异常: ", e);
        return Response.fail("参数校验失败").build();
    }

    /**
     * 全局异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponse handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Response.fail("请求失败").build();
    }

}

你可能感兴趣的:(Java架构之路- 统一异常处理)