springboot 自定义异常并捕获异常返给前端的实现代码

作者:---清心寡欲--- 时间:2022-07-23 03:09:52 

背景

在开发中,如果用try catch的方式,每个方法都需要单独实现,为了方便分类异常,返回给前端,采用了@ControllerAdvice注解和继承了RuntimeException的方式来实现。

实现内容

捕获了三类异常
1.业务异常

          BusinessException

2.系统异常 

        SystemException

3.其他异常

        利用@ExceptionHandler(RuntimeException.class)去捕获

ExceptionAdvice类捕获以上三类异常,并返回自定义类型格式数据

实现代码

业务异常BusinessException类实现方式,继承RuntimeException


public class BusinessException extends  RuntimeException {
/**
* 错误编码
*/
private String code;

public BusinessException() {
super();
}

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

public BusinessException(String code, String message) {
super(message);
this.code = code;
}

public BusinessException(Throwable cause) {
super(cause);
}

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

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

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

@Override
public String getMessage() {
return super.getMessage();
}

@Override
public String toString() {
return this.code + ":" + this.getMessage();
}
}

系统异常SystemException类实现方式,继承RuntimeException,同业务异常类的实现方式一样


public class SystemException extends  RuntimeException {
/**
* 错误编码
*/
private String code;

public SystemException() {
super();
}

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

public SystemException(String code, String message) {
super(message);
this.code = code;
}

public SystemException(Throwable cause) {
super(cause);
}

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

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

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

@Override
public String getMessage() {
return super.getMessage();
}

@Override
public String toString() {
return this.code + ":" + this.getMessage();
}
}

ExceptionAdvice类,采用增强Controller注解 @ControllerAdvice的方式来实现

1.方法名称和返回类型都可以根据自己需要定义

2.采用注解@ExceptionHandler,就是捕获的异常类型,我们只需要把需要捕获异常类型写进来就好

springboot 自定义异常并捕获异常返给前端的实现代码

 ExceptionAdvice 具体代码实现如下:


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionAdvice {
   public static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

@ResponseBody
   @ExceptionHandler(SystemException.class)
   public Result handleException(Exception e) {
       logger.error("系统异常信息:", e);
       Result result = new Result();
       if (e instanceof BusinessException) {
           e = (BusinessException) e;
           result.setCode(((BusinessException) e).getCode());
       }
       result.setFailed(e.getMessage());
       return result;
   }

@ExceptionHandler(RuntimeException.class)
   @ResponseBody
   public Result handleException(RuntimeException e) {
       logger.error("异常信息:", e.getMessage());
       Result result = new Result();
       result.setStatus(500);
       result.setMessage(e.getMessage());
       return result;
   }

@ExceptionHandler(BusinessException.class)
   @ResponseBody
   public AjaxJson doBusinessException(Exception e) {
       AjaxJson ajaxJson = new AjaxJson();
       logger.error("业务异常消息:", e.getMessage());
       ajaxJson.setRet(-1);
       ajaxJson.setMsg(e.getMessage());
       return ajaxJson;
   }

}

测试代码

1.我们捕获一个业务异常BusinessException,输出aaa

springboot 自定义异常并捕获异常返给前端的实现代码

 springboot 自定义异常并捕获异常返给前端的实现代码

2.捕获系统异常


throw new SystemException("aaaa");

3.其他的try catch的异常,这个就可以捕获了 

springboot 自定义异常并捕获异常返给前端的实现代码

来源:https://blog.csdn.net/ying456baby/article/details/121291964

标签:springboot,自定义,异常
0
投稿

猜你喜欢

  • Flutter runApp到渲染上屏分析详解

    2023-06-27 12:09:45
  • 阿里、华为、腾讯Java技术面试题精选

    2023-11-25 02:29:39
  • java中Hibernate缓存形式总结

    2023-10-19 15:57:43
  • Spring5中的WebClient使用方法详解

    2023-08-05 14:50:24
  • Java 回调callback举例详解

    2023-11-11 16:25:09
  • 教你使用Java获取当前时间戳的详细代码

    2021-09-19 04:41:02
  • 怎么把本地jar包放入本地maven仓库和远程私服仓库

    2023-12-05 20:13:00
  • 如何用注解的方式实现Mybatis插入数据时返回自增的主键Id

    2022-02-05 09:29:55
  • Java实现简单树结构

    2023-08-06 18:59:12
  • Springboot整合Shiro的代码实例

    2021-09-03 04:16:52
  • SpringBoot整合mybatis-plus进阶详细教程

    2023-11-27 05:13:12
  • 详解java中String、StringBuilder、StringBuffer的区别

    2023-06-17 06:03:23
  • Java8新特性:函数式编程

    2021-12-01 03:09:02
  • 一文搞懂MyBatis多数据源Starter实现

    2023-07-19 03:34:22
  • IntelliJ IDEA快速创建getter和setter方法

    2023-06-04 00:44:46
  • C#实现截图工具小项目

    2023-10-02 08:20:03
  • SpringCloud之微服务容错的实现

    2023-11-29 02:02:22
  • Spring IOC与DI核心重点分析

    2023-11-12 14:35:55
  • 简单了解Java断言利器AssertJ原理及用法

    2022-07-12 15:54:21
  • 一文搞懂Mybatis-plus的分页查询操作

    2023-11-25 10:23:17
  • asp之家 软件编程 m.aspxhome.com