前言
软件开发springboot项目过程中,不可避免的需要处理各种异常,spring mvc 架构中各层会出现大量的try {...} catch {...} finally {...} 代码块,不仅有大量的冗余代码,而且还影响代码的可读性。这样就需要定义个全局统一异常处理器,以便业务层再也不必处理异常。
推荐理由
代码复制到项目中通过简单的配置即可实现 可以灵活的根据自己的业务异常进行更细粒度的扩展 实践
1 封装统一返回结果类
源代码 public class ajaxresult { //是否成功 private boolean success; //状态码 private integer code; //提示信息 private string msg; //数据 private object data; public ajaxresult() { } //自定义返回结果的构造方法 public ajaxresult(boolean success,integer code, string msg,object data) { this.success = success; this.code = code; this.msg = msg; this.data = data; } //自定义异常返回的结果 public static ajaxresult defineerror(businessexception de){ ajaxresult result = new ajaxresult(); result.setsuccess(false); result.setcode(de.geterrorcode()); result.setmsg(de.geterrormsg()); result.setdata(null); return result; } //其他异常处理方法返回的结果 public static ajaxresult othererror(errorenum errorenum){ ajaxresult result = new ajaxresult(); result.setmsg(errorenum.geterrormsg()); result.setcode(errorenum.geterrorcode()); result.setsuccess(false); result.setdata(null); return result; } public boolean getsuccess() { return success; } public void setsuccess(boolean success) { this.success = success; } public integer getcode() { return code; } public void setcode(integer code) { this.code = code; } public string getmsg() { return msg; } public void setmsg(string msg) { this.msg = msg; } public object getdata() { return data; } public void setdata(object data) { this.data = data; } } 2 自定义异常封装类 源码: public class businessexception extends runtimeexception { private static final long serialversionuid = 1l; /** * 错误状态码 */ protected integer errorcode; /** * 错误提示 */ protected string errormsg; public businessexception(){ } public businessexception(integer errorcode, string errormsg) { this.errorcode = errorcode; this.errormsg = errormsg; } public integer geterrorcode() { return errorcode; } public void seterrorcode(integer errorcode) { this.errorcode = errorcode; } public string geterrormsg() { return errormsg; } public void seterrormsg(string errormsg) { this.errormsg = errormsg; } } 3 错误枚举,拒绝硬编码 源码 public enum errorenum { // 数据操作错误定义 success(200, 成功), no_permission(403,你没得权限), no_auth(401,未登录), not_found(404, 未找到该资源!), internal_server_error(500, 服务器异常请联系管理员), ; /** 错误码 */ private integer errorcode; /** 错误信息 */ private string errormsg; errorenum(integer errorcode, string errormsg) { this.errorcode = errorcode; this.errormsg = errormsg; } public integer geterrorcode() { return errorcode; } public string geterrormsg() { return errormsg; } } 4 全局异常处理类 源码 /** * 全局异常处理器 * */ @restcontrolleradvice public class globalexceptionhandler { private static final logger log = loggerfactory.getlogger(globalexceptionhandler.class); /** * 处理自定义异常 */ @exceptionhandler(value = businessexception.class) public ajaxresult bizexceptionhandler(businessexception e) { log.error(e.getmessage(), e); return ajaxresult.defineerror(e); } /** * 处理其他异常 */ @exceptionhandler(value = exception.class) public ajaxresult exceptionhandler( exception e) { log.error(e.getmessage(), e); return ajaxresult.othererror(errorenum.internal_server_error); } } 5 测试
返回结果:
一加5什么时候上市?一加5什么配置?一加5的最新消息:8G运存 全面屏
一文详解变频器和PLC/PCA系统
针对毫米波应用的电路材料选择
层状LiNi0.8Co0.1Mn0.1O2的容量损失和结构退化之间的关系
人工智能领域多模态的概念和应用场景
公司这套架构统一处理try...catch真香!
M2还未发布就有M3消息?据称苹果新款iMac将搭载M3芯片
测量CDMA2000和W-CDMA高功率放大器中的射频功率
MGA-635P8超低噪声放大器特性分析
小米6S首发!自研的松果处理器八核心A53:媲美骁龙835
电阻也能起到隔离的作用吗?
支持热插拔功能的接口有哪些,不支持热插拔的接口类型是什么
uPD16311在DVD前面板控制电路中的应用
无人值守称重系统地磅汽车衡运用称重传感器的原理及应用
炫科技!未来公寓楼阳台可停放无人驾驶飞机
触想二代系列触控显示设备的应用优势分享
第十四节 Lora
汽车应用的超薄LED背光照明模块介绍
微软要求修改联合国文档 称Linux是反商业的
美的IoT谈智能家居新趋势 在智能家居领域取得一系列成绩