SpringBoot全局异常拦截

时间:2021-07-23 22:21:38

SpringBoot全局异常捕获

使用到的技能

@RestControllerAdvice或(@ControllerAdvice+@ResponseBody)

@ExceptionHandler

代码实现

统一返回入口

自定义异常

全局异常处理

统一返回入口Result

/**

  • @ClassName Result

  • @Description 统一返回格式

  • @Author chaoba

  • @Version V1.0

    */

    @Data

    public class Result {

    private int code;

    private String msg;

    private T data;

    /**

    • 成功时候的调用

      */

      public static Result success(T data) {

      Result result = new Result(CodeMsg.SUCCESS);

      result.setData(data);

      return result;

      }

    /**

    • 成功时候的调用

      */

      public static Result successMsg(String msg) {

      return new Result(200, msg);

      }

    /**

    • 失败时候的调用

      */

      public static Result errorCodeMsg(CodeMsg codeMsg) {

      return new Result(codeMsg);

      }

    /**

    • 成功时候的调用

      */

      public static Result successCodeMsg(CodeMsg codeMsg) {

      return new Result(codeMsg);

      }

    public static Result error(T data) {

    Result result = new Result(CodeMsg.FAIL);

    result.setData(data);

    return result;

    }

    /**

    • 失败时候的调用

      */

      public static Result errorMsg(String msg) {

      return new Result(0, msg);

      }

    /**

    • 全部参数
    • @param
    • @param
    • @return

      */

      public static Result getResult() {

      return new Result();

      }

    public static Result toAjaxResult(int rows) {

    return rows > 0 ? success(null) : error(null);

    }

    public static Result toAjaxResult(boolean rows) {

    return rows ? success(null) : error(null);

    }

    private Result(T data) {

    this.data = data;

    }

    private Result() {

    }

    private Result(int code, String msg) {

    this.code = code;

    this.msg = msg;

    }

    private Result(CodeMsg codeMsg) {

    if (codeMsg != null) {

    this.code = codeMsg.getCode();

    this.msg = codeMsg.getMsg();

    }

    }

}

自定义异常

/**

  • @ClassName GlabalException

  • @Description 全局异常

  • @Author chaoba

  • @Version V1.0

    */

    @Data

    public class GlabalException extends RuntimeException {

    //错误状态码

    private int code;

    public GlabalException(int code) {

    this.code = code;

    }

    public GlabalException(String message, int code) {

    super(message);

    this.code = code;

    }

    public GlabalException(CodeMsg codeMsg) {

    super(codeMsg.getMsg());

    this.code = codeMsg.getCode();

    }

    public GlabalException(String message) {

    super(message);

    }

    public GlabalException(Throwable cause, int code) {

    super(cause);

    this.code = code;

    }

    public GlabalException(GlabalException ex) {

    super(ex);

    }

    }

    全局异常处理

    /**

  • @ClassName GlabalExceptionHandler

  • @Description 全局异常拦截,只提供接口-无需页面404等错误

  • @Author chaoba

  • @Version V1.0

    */

    @RestControllerAdvice

    public class GlabalExceptionHandler {

    //拦截自定义异常

    @ExceptionHandler(value = GlabalException.class)

    public Result jsonErrorHandler(GlabalException e) {

    Result

}

本文作者: 暮雪超霸

本文链接:https://www.cnblogs.com/chaoba/p/14204069.html