SpringBoot配置GlobalExceptionHandler全局异常处理器案例
作者:strive_day 时间:2023-06-11 12:14:36
1. 创建全局异常处理器类GlobalExceptionHandler
@ControllerAdvice: 定义统一的异常处理类,捕获 Controller 层抛出的异常。如果添加 @ResponseBody 返回信息则为JSON格式,这样就不必在每个Controller中逐个定义AOP去拦截处理异常。
@RestControllerAdvice: 相当于 @ControllerAdvice 与 @ResponseBody 的结合体。
@ExceptionHandler: 统一处理一种类的异常,减少代码重复率,降低复杂度。
@ControllerAdvice
public class GlobalExceptionHandler {
//@ExceptionHandler 该注解声明异常处理方法, ModelAndView mv
@ExceptionHandler(value = Exception.class)
public ModelAndView myHandler(Exception e, HttpServletRequest request, HttpServletResponse response) {
System.out.println("GlobalExceptionHandler全局异常处理器捕获");
ModelAndView mv = new ModelAndView();
mv.addObject("message", e.getMessage()); //异常错误信息提示
mv.addObject("url", request.getRequestURI()); //异常请求的url地址
mv.addObject("status", response.getStatus()); //获取状态码
mv.setViewName("/pages/exception/error"); //异常的视图名称
return mv;
}
}
【注意】基于@ControllerAdvice注解的全局异常统一处理只能针对于Controller层的异常。也就是只能捕获到Controller层的异常,在service层或者其他层面的异常都不能捕获。
2. 创建controller测试出现异常情况
//测试异常处理
@GetMapping(path = "/exception")
public String toException() {
System.out.println("toException");
//throw new Exception();
int i =1/0;
System.out.println("toException end");
return "/pages/company/company_list";
}
编写html页面显示错误信息
<!-- 统一异常处理页面 -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
<title>出现错误啦</title>
</head>
<body>
<div class="content-container">
<div class="head-line">
<img src="../img/error.jpg" alt="" width="120"/>
</div>
<div class="subheader">
<span name="message" th:text="${status}"/>,页面走丢啦<br/>
<p style="font-size: 16px">
原因:<font color="red" size="20px"><span name="message" th:text="${message}"/></font><br/>
地址:<a th:href="${url}" rel="external nofollow" ><span name="url" th:text="${url}"/></a><br/>
</p>
</div>
<div class="hr"></div>
<div class="context">
<p>您可以返回上一页重试,或直接向我们反馈错误报告
<br/>
联系地址:<a href="https://striveday.blog.csdn.net/" rel="external nofollow" >String_day</a><br/>
联系电话:<span>18828886888</span>
</p>
</div>
</body>
</html>
访问错误查看跳转页面
http://localhost:8000/OnlineMall/page/exception
来源:https://blog.csdn.net/qq_40542534/article/details/110691409
标签:SpringBoot,GlobalExceptionHandler,全局异常
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解使用Spring Security进行自动登录验证
2021-07-28 12:31:51
![](https://img.aspxhome.com/file/2023/8/72288_0s.jpg)
C#泛型集合类System.Collections.Generic
2023-02-24 19:21:18
idea中如何去掉不想commit的文件
2021-11-09 15:51:30
![](https://img.aspxhome.com/file/2023/0/113480_0s.png)
Quartz.Net调度框架配置解析
2021-10-13 01:35:02
关于C++一些特性的探究
2022-04-10 07:17:54
Android使用Gradle依赖配置compile、implementation与api的区别介绍
2023-09-27 18:11:40
Flutter 构建一个常用的页面框架
2022-10-02 02:54:19
![](https://img.aspxhome.com/file/2023/0/114370_0s.jpg)
Android实现银行卡号扫描识别功能
2021-09-05 01:56:50
关于springboot中对sqlSessionFactoryBean的自定义
2022-12-09 06:05:09
Java实现学生管理系统(控制台版本)
2023-04-11 04:12:05
android webview中使用Java调用JavaScript方法并获取返回值
2022-03-01 20:00:57
基于SpringMVC的全局异常处理器介绍
2023-11-23 20:55:58
Android开发中MotionEvent坐标获取方法分析
2022-03-29 02:43:42
![](https://img.aspxhome.com/file/2023/0/93280_0s.png)
Android实现一个带粘连效果的LoadingBar
2023-04-26 11:47:41
![](https://img.aspxhome.com/file/2023/8/114288_0s.gif)
Android中资源文件用法简单示例
2023-09-24 22:29:04
详解Java中方法重写和方法重载的6个区别
2023-11-28 13:42:18
![](https://img.aspxhome.com/file/2023/2/60072_0s.jpg)
Android 偷拍功能实现(手机关闭依然拍照)详解及实例代码
2023-01-08 00:23:19
![](https://img.aspxhome.com/file/2023/9/114289_0s.png)
Android Volley框架全面解析
2022-05-11 15:33:15
![](https://img.aspxhome.com/file/2023/8/126668_0s.jpg)
Android编程创建桌面快捷方式的常用方法小结【2种方法】
2023-12-07 00:50:35
简介Winform中创建用户控件
2021-10-14 04:39:37