项目使用 GlobalExceptionHandler 自定义异常 一

博主原创,未经允许不得转载:

每个项目都有自己的一套异常类的定义。总结一下,项目中使用自定义异常比较好的封装。

  1.定义项目中统一使用的异常类,用于捕获项目中的自定义异常等:

package com.common;

/**
 * 自定义异常
 */
public class CustomException extends Exception {

    private static final long serialVersionUID = 8984728932846627819L;

    public CustomException() {
        super();
    }
    
    public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    /**
     * @param message
     * @param cause
     */
    public CustomException(String message, Throwable cause) {
        super(message, cause);
    }

    /**
     * @param message
     */
    public CustomException(String message) {
        super(message);
    }

    /**
     * @param cause
     */
    public CustomException(Throwable cause) {
        super(cause);
    }
}

2.定义异常捕获后返回给客户端的统一数据格式的实体类:

@Data
public class ErrorResult{
    // 错误码
    private Integer errorCode;
    
    // 错误描述
    private String errorMsg;
}

3.使用 @RestControllerAdvice注解,自定义异常捕获的基类,处理和封装捕获的异常信息。

 看 @RestControllerAdvice 源码可以知道,它就是@ControllerAdvice和@ResponseBody的合并。此注解通过对异常的拦截实现的统一异常返回处理。

它通常用于定义@ExceptionHandler, @InitBinder 和 @ModelAttribute 适用于所有@RequestMapping方法的方法。

创建一个 GlobalExceptionHandler 类,并添加上 @RestControllerAdvice 注解就可以定义出异常通知类了,然后在定义的方法中添加上 @ExceptionHandler 即可实现异常的捕捉

import com.baizhi.exception.CustomException;
import com.baizhi.exception.ErrorResult;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@RestControllerAdvice
public class GlobalExceptionHandler{
    /**
     * 定义要捕获的异常 可以多个 @ExceptionHandler({})
     *
     * @param request  request
     * @param e        exception
     * @param response response
     * @return 响应结果
     */
    @ExceptionHandler(CustomException.class)
    public ErrorResult customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        CustomException exception = (CustomException) e;
        return new ErrorResult(exception.getCode(), exception.getMessage());
    }
 
    /**
     * 捕获  RuntimeException 异常
     * TODO  如果你觉得在一个 exceptionHandler 通过  if (e instanceof xxxException) 太麻烦
     * TODO  那么你还可以自己写多个不同的 exceptionHandler 处理不同异常
     *
     * @param request  request
     * @param e        exception
     * @param response response
     * @return 响应结果
     */
    @ExceptionHandler(RuntimeException.class)
    public ErrorResult runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        RuntimeException exception = (RuntimeException) e;
        return new ErrorResult(400, exception.getMessage());
    }
}
原文地址:https://www.cnblogs.com/zjdxr-up/p/14040044.html