智能摘要文章源自JAVA秀-https://www.javaxiu.com/37954.html
这样就需要定义个全局统一异常处理器,以便业务层再也不必处理异常。SUCCESS(200,"成功"),NO_PERMISSION(403,"你没得权限"),NO_AUTH(401,"未登录"),NOT_FOUND(404,"未找到该资源!文章源自JAVA秀-https://www.javaxiu.com/37954.html
原文约 1099 字 | 图片 8 张 | 建议阅读 3 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/37954.html
统一处理 try...catch 这么香,求求你不要再满屏写了!
戳一戳→ 程序员的成长之路 文章源自JAVA秀-https://www.javaxiu.com/37954.html
文章源自JAVA秀-https://www.javaxiu.com/37954.html
程序员的成长之路文章源自JAVA秀-https://www.javaxiu.com/37954.html
互联网/程序员/技术/资料共享 文章源自JAVA秀-https://www.javaxiu.com/37954.html
关注文章源自JAVA秀-https://www.javaxiu.com/37954.html
阅读本文大概需要 2.8 分钟。文章源自JAVA秀-https://www.javaxiu.com/37954.html
作者:小李子说程序文章源自JAVA秀-https://www.javaxiu.com/37954.html
https://urlify.cn/6naQjq文章源自JAVA秀-https://www.javaxiu.com/37954.html
前言
软件开发springboot项目过程中,不可避免的需要处理各种异常,spring mvc 架构中各层会出现大量的try {...} catch {...} finally {...}代码块,不仅有大量的冗余代码,而且还影响代码的可读性。文章源自JAVA秀-https://www.javaxiu.com/37954.html
这样就需要定义个全局统一异常处理器,以便业务层再也不必处理异常。文章源自JAVA秀-https://www.javaxiu.com/37954.html
推荐理由
代码复制到项目中通过简单的配置即可实现文章源自JAVA秀-https://www.javaxiu.com/37954.html
可以灵活的根据自己的业务异常进行更细粒度的扩展文章源自JAVA秀-https://www.javaxiu.com/37954.html
实践
1 封装统一返回结果类
文章源自JAVA秀-https://www.javaxiu.com/37954.html
源代码文章源自JAVA秀-https://www.javaxiu.com/37954.html
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 自定义异常封装类
文章源自JAVA秀-https://www.javaxiu.com/37954.html
源码:文章源自JAVA秀-https://www.javaxiu.com/37954.html
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 错误枚举,拒绝硬编码
文章源自JAVA秀-https://www.javaxiu.com/37954.html
源码文章源自JAVA秀-https://www.javaxiu.com/37954.html
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 全局异常处理类
文章源自JAVA秀-https://www.javaxiu.com/37954.html
源码文章源自JAVA秀-https://www.javaxiu.com/37954.html
/** * 全局异常处理器 * */@RestControllerAdvicepublic 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 测试
文章源自JAVA秀-https://www.javaxiu.com/37954.html
返回结果:文章源自JAVA秀-https://www.javaxiu.com/37954.html
文章源自JAVA秀-https://www.javaxiu.com/37954.html
<END>文章源自JAVA秀-https://www.javaxiu.com/37954.html
推荐阅读:文章源自JAVA秀-https://www.javaxiu.com/37954.html
最强代码生成器平台,杀疯了~文章源自JAVA秀-https://www.javaxiu.com/37954.html
面试官:Redis用过是吧?那你讲讲Redis都有哪些监控指标?文章源自JAVA秀-https://www.javaxiu.com/37954.html
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
文章源自JAVA秀-https://www.javaxiu.com/37954.html
获取方式:点个「在看」,点击上方小卡片,进入公众号后回复「面试题」领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/37954.html
朕已阅 文章源自JAVA秀-https://www.javaxiu.com/37954.html

评论