如何在SpringBoot中实现统一异常处理
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇文章主要介绍了如何在SpringBoot中实现统一异常处理的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何在SpringBoot中实现统一异常处理文章都会有所收获,
千家信息网最后更新 2025年11月11日如何在SpringBoot中实现统一异常处理
这篇文章主要介绍了如何在SpringBoot中实现统一异常处理的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何在SpringBoot中实现统一异常处理文章都会有所收获,下面我们一起来看看吧。
场景:针对异常处理,我们原来的做法是一般在最外层捕获异常即可,例如在Controller中
@Controllerpublic class HelloController { private static final Logger logger = LoggerFactory.getLogger(HelloController.class); @GetMapping(value = "/hello") @ResponseBody public Result hello() { try { //TODO 具体的逻辑省略…… } catch (Exception e) { logger.error("hello接口异常={}", e); return ResultUtil.success(-1, "system error", null); } return ResultUtil.success(0, "success", null); }}这样的话也能解决部分问题,但是无法获取到自己指定的异常,引入全局统一异常处理的话将会极大的改善代码,减少冗余代码的产生。
自定义异常类:注意要继承自RuntimeException而不是Exception,继承自Exception的话,当抛出自定义异常时spring事务不会回滚
public class GlobalException extends RuntimeException { private Integer code; //因为我需要将异常信息也返回给接口中,所以添加code区分 public GlobalException(Integer code,String message) { super(message); //把自定义的message传递个异常父类 this.code = code; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; }}自定义统一异常处理器:比较关键的两个注解@ControllerAdvice、@ExceptionHandler
@ControllerAdvicepublic class ExceptionHandle { @ResponseBody //因为我需要将抛出的异常返回给接口,所以加上此注解 @ExceptionHandler public Result handle(Exception e) { if (e instanceof GlobalException) { GlobalException ge = (GlobalException) e; return ResultUtil.success1(ge.getCode(), ge.getMessage()); } return ResultUtil.success1(-1, "system error!"); } }写个测试类测试下
@GetMapping(value = "/hello1")@ResponseBodypublic Result hello(@RequestParam(value = "age", defaultValue = "50", required = false) Integer age) throws GlobalException { if (age < 10) { throw new GlobalException(ConstantEnum.LESS10.getCode(), ConstantEnum.LESS10.getMsg()); } else if (age > 50) { throw new GlobalException(ConstantEnum.MORE50.getCode(), ConstantEnum.MORE50.getMsg()); } else { return ResultUtil.success1(0, "success"); }}把code、message封装在了ConstantEnum枚举里面,方便统一维护
public enum ConstantEnum { ERROR(-1, "system error!"), SUCCESS(100, "success"), LESS10(101, "自定义异常信息-我小于10岁"), MORE50(5001, "自定义异常信息-我大于50岁"); private Integer code; private String msg; public Integer getCode() { return code; } public String getMsg() { return msg; } ConstantEnum(Integer code, String msg) { this.code = code; this.msg = msg; }}关于"如何在SpringBoot中实现统一异常处理"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"如何在SpringBoot中实现统一异常处理"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
处理
统一
中实
信息
接口
知识
代码
内容
注解
篇文章
测试
这样的话
两个
事务
价值
做法
全局
关键
冗余
场景
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
公安网服务器命令安装
湖北服务器维修调试价格
数据库提交事务的方法有哪些
多数据库客户端
实时备份数据库 英语
幻塔如何修改服务器
b树数据库
sci文摘型数据库
前沿软件开发过程
查询程序的服务器地址
网络安全教育视频手抄报
阜阳专业软件开发
土地增值税 优惠 软件开发
潜江帅手软件开发有限公司官网
鱼人服务器联盟部落比例
网络安全设备的基本操作
彩票软件开发出租
服务器添加环回网卡
上海常用软件开发零售价格
数据库的rss 怎么用
杭州第三届网络安全技能大赛
南充网络技术哪家好
收到软件开发费几年
云南超频服务器技术参数
信息化软件开发值得推荐
mysql 使用数据库
数据库原理和应用教程
ctf网络安全大赛宣传语
青岛鼎信 软件开发 知乎
网络安全竞赛创新方法