一、SpringBoot 异常处理方式
SpringBoot 提供了五种异常处理方式
1.自定义错误页面
SpringBoot 默认提供了一套处理异常的机制。一旦程序出现了异常 ,SpringBoot 会向 /error 的 url 发送请求。在 springBoot 中提供了BasicExceptionController 来处理 /error 请求,然后跳转到默认显示异常的页面来展示异常信息。
如 果 我 们 需 要 将 所 有 的 异 常 统一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再 src/main/resources/templates 目录下创建 error.html 页面。
注意:名称必须叫 error。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>errorPage</title>
</head>
<body>
错误页面。
<span th:text="${exception}"></span>
</body>
</html>
2.@ExceptionHandle 注解处理异常
Controller
@Controller
public class DemoController {
@RequestMapping("/errorInfo")
//算数异常
public String errorInfo(){
String str = null;
str.length();
return "index";
}
@RequestMapping("/errorInfo2")
public String errorInfo2(){
int x = 10/0;
return "index";
}
/**
* java.lang.ArithmeticException
* 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视图的指定
* 参数 Exception e:会将产生异常对象注入到方法中
*/
@ExceptionHandler(value={java.lang.ArithmeticException.class})
public ModelAndView arithmeticExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView("errorInfo");
mv.addObject("error", e);
return mv;
}
/**
* java.lang.NullPointerException
* 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视图的指定
* 参数 Exception e:会将产生异常对象注入到方法中
*/
@ExceptionHandler(value={java.lang.NullPointerException.class})
public ModelAndView nullPointerExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView("errorInfo2");
mv.addObject("error", e);
return mv;
}
}
页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>errorPage</title>
</head>
<body>
算数异常:ArithmeticException。
<span th:text="${error}"></span>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>errorPage</title>
</head>
<body>
空指针异常:NullPointerException
<span th:text="${error}"></span>
</body>
</html>
3.@ControllerAdvice+@ExceptionHandler 注解处理异常
需要创建一个处理异常的全局异常类。在该类上需要添加
@ControllerAdvice 注解
@ControllerAdvice
public class GlobalException {
/**
* java.lang.ArithmeticException
* 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视
图的指定
* 参数 Exception e:会将产生异常对象注入到方法中
*/
@ExceptionHandler(value={java.lang.ArithmeticException.class})
public ModelAndView arithmeticExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView("errorInfo");
mv.addObject("error", e);
return mv;
}
/**
* java.lang.NullPointerException
* 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视
图的指定
* 参数 Exception e:会将产生异常对象注入到方法中
*/
@ExceptionHandler(value={java.lang.NullPointerException.class})
public ModelAndView nullPointerExceptionHandler(Exception e){
ModelAndView mv = new ModelAndView("errorInfo2");
mv.addObject("error", e);
return mv;
}
}
这种方法分开定义处理异常的Hander,可以将错误对象传递到错误提示页面,方便开发人员查看、调试。
4.配置 SimpleMappingExceptionResolver 处理异常
在全局异常类中添加一个方法完成异常的同一处理
/**
* 通过 SimpleMappingExceptionResolver 做全局异常处理
*/
@Configuration
public class GlobalException {
/**
* 该方法必须要有返回值。返回值类型必须是:
SimpleMappingExceptionResolver
*/
@Bean
public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
SimpleMappingExceptionResolver resolver = new
SimpleMappingExceptionResolver();
Properties mappings = new Properties();
/**
* mappings.put("java.lang.ArithmeticException", "error1");
* 参数一:异常的类型,注意必须是异常类型的全名
* 参数二:视图名称
*/
mappings.put("java.lang.ArithmeticException", "error1");
mappings.put("java.lang.NullPointerException","error2");
//设置异常与视图映射信息的
resolver.setExceptionMappings(mappings);
return resolver;
}
}
这种方法只用定义一个处理异常的Hander,不足之处是不能够将错误对象传递到错误提示页面,不便于开发人员查看、调试。
5.自定义 HandlerExceptionResolver 类处理异常
需 要 再 全 局 异 常 处 理 类 中 实 现
HandlerExceptionResolver 接口
/**
* 通过实现 HandlerExceptionResolver 接口做全局异常处理
*/
@Configuration
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response,Object handler,Exception ex) {
ModelAndView mv = null;
//判断不同异常类型,做通过穿入不同的 url 跳转到不同视图,
//也可以调用 mv.setViewName(“errorInfo”) 方法指定跳转的 URL
if(ex instanceof ArithmeticException){
mv = new ModelAndView("errorInfo");
}
if(ex instanceof NullPointerException){
mv = new ModelAndView("errorInfo2");
}
mv.addObject("error", ex.);
return mv;
}
}
这种方法解决了第三种和第四种方法缺点,只用定义一个处理异常的Hander,返回的 ModelAndView 对象可以将需要的异常对象传递至错误页面。
二、Spring Boot 整合 Junit进行测试
pom.xml文件添加junit包的依赖:
<!-- 添加 junit 的 jar 包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
编写SpringBoot 测试类:JunitTest :
/**
*@RunWith:启动器
*SpringJUnit4ClassRunner.class:让 junit 与 spring 环境进行整合
*
*@SpringBootTest(classes={App.class}) 作用:(App.class为springBoot启动类)
*1,表示当前类为 springBoot 的测试类
*2,加载 SpringBoot 启动类。启动springBoot
*
*junit 与 spring 整合
@Contextconfiguartion("classpath:applicationContext.xml")
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={App.class})
public class JunitTest {
@Autowired
private UserImpl userImpl;
@Test
public void testUser(){
this.userImpl.addUser();
System.out.println("测试成功");
}
}