08.Spring Boot 实战~Spring Boot异常处理和单元测试
本文是上一篇文章的后续,详情点击该链接
关于异常处理呢,在Spring Boot中提供了五种处理方式
自定义错误页面
在SpringBoot中已经默认的提供了一套异常处理机制。 一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。并且提供了一个名为BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息。
如 果 我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再 src/main/resources/templates 目录下创建 error.html 页面。注意:页面名称必须叫 error
只要出现异常就会自动跳到error.html这里,随便创建一个项目,故意写错路径或者故意弄错其他东西试一下就知道了。
通过@ExceptionHandler 注解处理异常
写在Controller里
@Controller
@RequestMapping("/alvin")
public class MyController {
@RequestMapping("/ShowMain")
public String Show(){
String str = null;
//铁定空指针异常,先制造一个再说.....
str.length();
return "main";
}
//空指针异常处理
@ExceptionHandler(value = {java.lang.NullPointerException.class} )
public ModelAndView nullpointExcepitonHandler(Exception e){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("err",e.toString());
modelAndView.setViewName("nullpoint");
return modelAndView;
}
}
然后视图页面接收发送过来的异常信息
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>nullpoint</title>
</head>
<body>
<!--/*@thymesVar id="err" type=""*/-->
<span th:text="${err}"></span>
</body>
</html>
通过@ControllerAdvice 与@ExceptionHandler 注解处理异常
我们先创建一个全局的异常处理类
@ControllerAdvice
public class GlobalException {
@ExceptionHandler(value = {java.lang.NullPointerException.class} )
public ModelAndView nullpointExcepitonHandler(Exception e){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("err",e.toString());
modelAndView.setViewName("nullpoint");
return modelAndView;
}
@ExceptionHandler(value = {java.lang.ArithmeticException.class} )
public ModelAndView arithmeticExceptionHandler(Exception e){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("err",e.toString());
modelAndView.setViewName("exception");
return modelAndView;
}
}
然后自己制造一个算数异常比如2/0跳转接收即可
<!DOCTYPE html>
<html lang="en"xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>exception</title>
</head>
<body>
<!--/*@thymesVar id="err" type=""*/-->
<span th:text="${err}"></span>
</body>
</html>
通过 SimpleMappingExceptionResolver 对象处理异常
我们在全局异常处理类当中方法数量会非常多,因为可能会需要处理多个不同的异常,而每当多需要处理一个就要多加一个方法。这样会导致方法太多,所以我们要只通过一个方法来处理不同的异常并跳转不同的页面
修改下刚才那个类
@ControllerAdvice
public class GlobalException {
//此方法返回值必须是SimpleMappingExceptionResolver对象
@Bean
public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
//参数一:异常类型,并且是全名参数二:视图名称
properties.put("java.lang.NullPointerException","nullpoint");
properties.put("java.lang.ArithmeticException","exception");
resolver.setExceptionMappings(properties);
return resolver;
}
}
通过自定义 HandlerExceptionResolver 对象处理异常
@ControllerAdvice
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView = new ModelAndView();
//判断不同异常类型,做不同视图的跳转
if(e instanceof NullPointerException){
modelAndView.setViewName("nullpoint");
}
if(e instanceof ArithmeticException){
modelAndView.setViewName("exception");
}
modelAndView.addObject("error",e.toString());
return modelAndView;
}
}
SpringBoot 整合 Junit 单元测试
在pom.xml添加启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!--junit-vintage-engine 提 供 了 Junit3 与 Junit4的 运行 平 台 -->
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
测试代码
@SpringBootTest
public class TestProject {
@Test
public void Menu(){
System.out.println("hello world");
}
}
这个时候我们可以看到,不用Main方法也能运行