先创建个简单的SpringBoot项目,有一个HelloController
@Controller
public class HelloController {
@RequestMapping("/hello")
public @ResponseBody String hello(@RequestParam String name) {
return "hello, " + name;
}
}
加入现在访问一个不存在的request mapping,那么页面会报错
这样是很不友好的,所以需要对异常进行处理
下面几种方式,页面显示出来的前提是整合了前端模板框架比如Thymeleaf
SpringBoot整合Thymeleaf
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
controller中通过Model向前端传值
@Controller
public class HelloController {
@RequestMapping("/index")
public String index(Model model) {
model.addAttribute("msg", "Hello, this is test Thymeleaf");
return "index"; //指定返回给index.html页面
}
}
前端页面通过Thymeleaf语法取值
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Thymeleaf testing</h1>
<span th:text="${error}"></span>
</body>
</html>
1. 自定义错误页面
SpringBoot默认的处理异常的机制:默认已经提供了一套处理异常的机制,一旦出现异常,SpringBoot会向/error的url发送请求。在SpringBoot中提供了一个叫BasicExceptionController来处理/error请求,然后跳转到默认显示异常的error.html界面。
在resources >> templates目录下创建error.html页面,或者针对错误码创建比如404.html页面,再次访问报错则会显示此页面
2. 通过@ExceptionHandler处理异常
针对特定的异常进行处理。
@Controller
public class HelloController {
@RequestMapping("/hello")
public @ResponseBody String hello(@RequestParam String name) {
return "hello, " + name;
}
@RequestMapping("/say")
public String say() {
String word = null;
System.out.println(word.length()); //模拟NullPointerException
return "hello";
}
@RequestMapping("/cal")
public String cal() {
int a = 1;
int b = 0;
System.out.println(a / b); //模拟ArithmeticException
return "计算";
}
// 如果当前类中出现空指针异常会跳转到本方法对应的view中
@ExceptionHandler(NullPointerException.class)
public ModelAndView nullPointerExceptionHandler(Exception e) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("error", e.getMessage());
modelAndView.setViewName("nullPointerException");
return modelAndView;
}
// 如果当前类中出现ArithmeticException会跳转到本方法对应的view中
@ExceptionHandler(ArithmeticException.class)
public ModelAndView arithmeticExceptionHandler(Exception e) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("error", e.getMessage());
modelAndView.setViewName("arithmeticException");
return modelAndView;
}
}
3. 定义全局异常
将所有异常提出来和业务代码分离,通过异常类来管理项目中所有异常
@ControllerAdvice
public class GlobalException {
// 如果当前类中出现空指针异常会跳转到本方法对应的view中
@ExceptionHandler(NullPointerException.class)
public ModelAndView nullPointerExceptionHandler(Exception e) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("error", e.getMessage());
modelAndView.setViewName("nullPointerException");
return modelAndView;
}
// 如果当前类中出现ArithmeticException会跳转到本方法对应的view中
@ExceptionHandler(ArithmeticException.class)
public ModelAndView arithmeticExceptionHandler(Exception e) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("error", e.getMessage());
modelAndView.setViewName("arithmeticException");
return modelAndView;
}
}
4. SimpleMappingExceptionResolver
通过系统提供的SimpleMappingExceptionResolver异常处理类,在配置类中定义异常和处理的映射mapping
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
@Bean
public SimpleMappingExceptionResolver getMappingExceptionResolver() {
System.out.println("loading SimpleMappingExceptionResolver");
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
Properties mappings = new Properties();
mappings.setProperty("java.lang.NullPointerException", "nullPointerException");
mappings.setProperty("java.lang.ArithmeticException", "arithmeticException");
resolver.setExceptionMappings(mappings);
return resolver;
}
}
5. 自定义全局异常理类
@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
System.out.println("全局自定义异常触发....");
ModelAndView modelAndView = new ModelAndView();
if (ex instanceof NullPointerException) {
modelAndView.setViewName("nullPointerException");
modelAndView.addObject("error", "空指针异常");
} else if (ex instanceof ArithmeticException) {
modelAndView.setViewName("arithmeticException");
modelAndView.addObject("error", "算数异常");
}
return modelAndView;
}
}