1. 自定义错误页面
SpringBoot 默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会像/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicExceptionController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息。
我们只需要在resources/templates
中添加一个error.html
页面即可
再次访问:
2. @ExceptionHandler处理
针对特定的异常处理。异常处理代码和业务代码耦合性比较强
2.1 控制器
package com.dbl.springboot.demo.exception.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* descrription:
* <p>
* Create by DbL on 2020/10/29 0029 7:53
*/
@Controller("/exception")
public class UserController {
@RequestMapping("/show1")
public String showInfo1(Model model) {
String msg = null;
msg.length(); // NullPointerException
return "success";
}
@RequestMapping("/show2")
public String showInfo2() {
int i = 0;
int b = 100;
System.out.println(b / i); // ArithmeicExpetion
return "success";
}
/**
* 如果当前类中出现了NullPointerException异常就会跳转到本方法对应的view中
*
* @return
*/
@ExceptionHandler(value = {NullPointerException.class})
public ModelAndView nullPointerExceptionHandler(Exception e) {
ModelAndView view = new ModelAndView();
view.addObject("error", e.toString());
view.setViewName("error1");
return view;
}
/**
* 如果当前类中出现了ArithmeticException异常就会跳转到本方法对应的view中
*
* @return
*/
@ExceptionHandler(value = {ArithmeticException.class})
public ModelAndView arithmeticExceptionHandler(Exception e) {
ModelAndView view = new ModelAndView();
view.addObject("error", e.toString());
view.setViewName("error2");
return view;
}
}
2.2 定制报错页面
<!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>系统错误页面1</h1>
<span th:text="${error}"></span>
</body>
</html>
2.3 测试
3. @ControllerAdvice处理
实现业务代码和系统异常处理代码解耦。先将2.1中异常处理注释。
3.1 新增全局异常控制类
package com.dbl.springboot.demo.exception.handler;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
/**
* descrription:
* <p>
* Create by DbL on 2020/11/3 0003 7:29
*/
@ControllerAdvice
public class GlobalException {
/**
* 如果当前类中出现了NullPointerException异常就会跳转到本方法对应的view中
* @return
*/
@ExceptionHandler(value = {NullPointerException.class})
public ModelAndView nullPointerExceptionHandler(Exception e){
ModelAndView view = new ModelAndView();
view.addObject("error",e.toString());
view.setViewName("error1");
return view;
}
/**
* 如果当前类中出现了ArithmeticException异常就会跳转到本方法对应的view中
* @return
*/
@ExceptionHandler(value = {ArithmeticException.class})
public ModelAndView arithmeticExceptionHandler(Exception e){
ModelAndView view = new ModelAndView();
view.addObject("error",e.toString());
view.setViewName("error2");
return view;
}
}
4.SimpleMappingExceptionResolver处理
通过系统提供的异常映射处理实现。
4.1 在启动类中配置映射
package com.dbl.springboot.demo;
import com.dbl.springboot.demo.filter.SecondFilter;
import com.dbl.springboot.demo.listener.SecondListener;
import com.dbl.springboot.demo.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import java.util.Properties;
@SpringBootApplication
// 在SpringBoot 启动的时候回自动去扫描WebServlet注解
@ServletComponentScan
public class GpDblSpringbootServletApplication {
public static void main(String[] args) {
SpringApplication.run(GpDblSpringbootServletApplication.class, args);
}
/**
* 异常信息和对应的 处理地址的 映射
* @return
*/
@Bean
public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
SimpleMappingExceptionResolver mapping = new SimpleMappingExceptionResolver();
Properties mappings = new Properties();
mappings.setProperty("java.lang.NullPointerException","error3");
mappings.setProperty("java.lang.ArithmeticException","error4");
mapping.setExceptionMappings(mappings);
return mapping;
}
}
4.2 测试
5 自定义HandlerExceptionResolver
5.1 自定义MyHandlerExceptionResolver 类
package com.dbl.springboot.demo.exception.handler;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* descrription:
* <p>
* Create by DbL on 2020/11/3 0003 7:42
*/
@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
/**
* 自定义的全局异常
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param e
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest
, HttpServletResponse httpServletResponse
, Object o, Exception e) {
System.out.println("全局的自定义异常处理触发了....");
ModelAndView mv = new ModelAndView();
if(e instanceof NullPointerException){
mv.setViewName("error1");
mv.addObject("error","空指针异常");
}else if(e instanceof ArithmeticException){
mv.setViewName("error2");
mv.addObject("error","算数异常");
}
return mv;
}
}