springboot 自定义异常并捕获异常返给前端的实现代码
作者:---清心寡欲--- 发布时间:2022-07-23 03:09:52
标签:springboot,自定义,异常
背景
在开发中,如果用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,就是捕获的异常类型,我们只需要把需要捕获异常类型写进来就好
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
2.捕获系统异常
throw new SystemException("aaaa");
3.其他的try catch的异常,这个就可以捕获了
来源:https://blog.csdn.net/ying456baby/article/details/121291964


猜你喜欢
- 本文为大家整理了C#图片切割、图片压缩、缩略图生成的实现代码,大家可以收藏,方便以后使用,具体内容如下/// 图片切割函数 /// <
- 本文实例为大家分享了java使用Cookie判断用户登录情况的方法,供大家参考,具体内容如下1.判断是否登录public boolean i
- 在Word中可将文件通过OLE对象嵌入的方式插入到文档,包括Word、excel、PDF、PPT、图片、宏文件、文件包等在内的多种文件类型。
- 一般而言在Android上使用JAVA实现彩图转换为灰度图,与J2ME上的实现方法类似,不过遇到频繁地转换或者是大图转换时,就必须使用NDK
- 前言Android12 有很多令人惊喜的变化,比如基于 Material You 的全新 UI,基于 SplashScreen 的应用启动画
- 如何在listview中插入图片,相信大家很想知道,下面就为大家分享具体步骤:第一步:在窗体中拖入ListView控件和imageList控
- 前言此前部门内的一个线上系统上线后内存一路飙高、一段时间后直接占满。协助开发人员去分析定位,发现内存中某个Object的量远远超出了预期的范
- 手写一个通用加载中、显示数据、加载失败、空数据的LoadingView框架。定义3个布局:加载中,加载失败,空数据加载中:<?xml
- #include <algorithm>class TestClass{public: int Sub(int x,
- FileWriter/FileReader介绍:FileWriter 类从 OutputStreamWriter 类继承而来。该类按字符向流
- 前言文件的上传和下载都是基于io复制,只不过文件上传是浏览器向服务器发送报文文件下载是服务器向浏览器发送报文提示:以下是本篇文章正文内容,下
- 1、在ActionSupport中有一个validate()方法,这个方法是验证方法,它会在execute()方法执行之前执行,所以能够起到
- 有些SIM卡在出厂时并没有预置VoiceMail number,但运营商又要求能够根据PLMN去自适应的从手机中读取到预设的VM numbe
- springboot读取配置文件到静态工具类通常我们读取配置文件可以用@Value注解和@Configuration,@Configurat
- 需求:如上图,需要隐藏右上角的多用户蓝色图标.修改后结果如下:锁屏相关的布局在SystemUIframeworks/base/package
- 1.获取Return返回值//存储过程//Create PROCEDURE MYSQL//
- 在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来
- 我们都知道在一些特殊的场景,我们需采用特殊的编码格式,如:UTF-8,但是系统默认的编码为ISO-8859-1那么我们就需要将编码转换为我们
- 闲来无事,刚学会把git部署到远程服务器,没事做,所以简单做了一个抓取网页信息的小工具,里面的一些数值如果设成参数的话可能扩展性能会更好!希
- 目录原理实战原理其原理如图:1.配置数据源信息(包括表名)2.读取数据表字段信息:列名、类型、字段注释、表注释3.编写代码模板,并将该模板加