SpringBoot 中对于异常处理提供了五种处理方式
1. 自定义错误页面
SpringBoot 默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会像/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicExceptionController 来处理 /error 请求,然后跳转到默认显示异常的页面来展示异常信息。
controller:
@Controller
public class UsersController {
/**
* java.lang.NullPointerException
*/
@RequestMapping("/addUser")
public String addUser(Users users){
String str = null;
str.length();
return "add";
}
/**
* java.lang.ArithmeticException
*/
@RequestMapping("/getUser")
public String getAllUser(){
int i = 10/0;
return "getAll";
}
}
如 果 我 们 需 要 将 所 有 的 异 常 同 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 在 src/main/resources/templates 目录下创建 error.html 页面。注意:名称必须叫 error
error.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>我的错误页面</title>
</head>
<body>
<h4>
走丢了。。。。
<span th:text="${exception}"></span>
</h4>
</body>
</html>
运行结果:
2. @ExceptionHandle 注解处理异常
特点:只对当前 controller 有作用
controller:
@Controller
public class UsersController {
/**
* java.lang.NullPointerException
*/
@RequestMapping("/addUser")
public String addUser(Users users){
String str = null;
str.length();
return "add";
}
/**
* java.lang.ArithmeticException
*/
@RequestMapping("/getUser")
public String getAllUser(){
int i = 10/0;
return "getAll";
}
@ExceptionHandler(value = {java.lang.ArithmeticException.class})
public ModelAndView getNullPointerException(Exception e){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("error",e.getMessage());
modelAndView.setViewName("error1");
return modelAndView;
}
@ExceptionHandler(value = {java.lang.NullPointerException.class})
public ModelAndView getArithmeticException(Exception e){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("error",e.getMessage());
modelAndView.setViewName("error2");
return modelAndView;
}
}
错误页面:
error1:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>我的错误页面</title>
</head>
<body>
<h4>
嘤嘤嘤。。。。
<span th:text="${error}"></span>
</h4>
</body>
</html>
error2:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>我的错误页面</title>
</head>
<body>
<h4>
出错啦。。。。
<span th:text="${error}"></span>
</h4>
</body>
</html>
运行结果:
3. @ControllerAdvice + @ExceptionHandler 注解处理异常
需要创建一个能够处理异常的全局异常类。在该类上需要添加 @ControllerAdvice 注解
@ControllerAdvice
public class GlobalException {
@ExceptionHandler(value = {java.lang.ArithmeticException.class})
public ModelAndView getNullPointerException(Exception e){
ModelAndView modelAndView = new ModelAndView();
System.out.println("Arildfsl____"+e.getMessage());
modelAndView.addObject("error",e.getMessage());
modelAndView.setViewName("error1");
return modelAndView;
}
@ExceptionHandler(value={java.lang.NullPointerException.class})
public ModelAndView nullPointerExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error2");
return mv;
}
}
4. 配置 SimpleMappingExceptionResolver 处理异常
在全局异常类中添加一个方法完成异常的同一处理。
特点: 无法获取异常信息
@ControllerAdvice
public class GlobalException {
@Bean
public SimpleMappingExceptionResolver mappingExceptionResolver (){
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
/**
* 参数一:异常类型
* 参数二:视图名称
*/
properties.put("java.lang.ArithmeticException","error1");
properties.put("java.lang.NullPointerException","error2");
// 设置异常与视图的映射
resolver.setExceptionMappings(properties);
return resolver;
}
}
5. 自定义 HandlerExceptionResolver 类处理异常
需 要 再 全 局 异 常 处 理 类 中 实 现 HandlerExceptionResolver 接口
@ControllerAdvice
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView mv = new ModelAndView();
// 根据不同的异常类型跳转到不同的错误页面
if (e instanceof NullPointerException){
mv.setViewName("error2");
}else if(e instanceof ArithmeticException){
mv.setViewName("error1");
}
mv.addObject("error",e.toString());
return mv;
}
}
项目文件结构:
版权声明:本文为qq_41573234原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。