1.自定义错误页面
SpringBoot 默认的处理异常机制:SpringBoot默认的已经提供了一套处理异常的机制。一旦程序中出现了异常,SpringBoot会向/error的url发送请求。在SpringBoot中提供了一个叫BasicExceptionController来处理/error请求,然后跳转到默认显示异常的页面来展示异常信息。
如果需要将所有的异常同一跳转到自定义的错误页面,需要再src /main/resources/templates目录下创建error.html页面。注意:名称必须叫error.
2.@ExceptionHandle注解处理异常
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo() {
String string = null;
string.length();
return "index";
}
@RequestMapping("/show2")
public String showInfo2() {
int a = 10 / 0;
return "index";
}
/**
* java.lang.ArithmeticException 该方法需要返回一个ModelAndView :目的是可以让我们封装异常信息以及视图指定
* Exception e : 会将产生异常对象注入到方法中
*
* @param e
* @return
*/
@ExceptionHandler(value = { java.lang.ArithmeticException.class })
public ModelAndView arithmeticExceptionHandler(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error1");
return mv;
}
/**
* java.lang.ArithmeticException 该方法需要返回一个ModelAndView :目的是可以让我们封装异常信息以及视图指定
* Exception e : 会将产生异常对象注入到方法中
* @param e
* @return
*/
@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;
}
}
3.@ControllerAdvice+@ExceptionHandler注解处理异常
@ControllerAdvice
public class GlobalException {
@ExceptionHandler(value = { java.lang.ArithmeticException.class })
public ModelAndView arithmeticExceptionHandler(Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("error", e.toString());
mv.setViewName("error1");
return mv;
}
@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处理异常
/**
* 通过SimpleMappingExceptionResolver做全局异常处理
*
* @author Lenovo
*
*/
@Configuration
public class GlobalException {
/**
* 该方法必须有返回值类型,返回值类型为:SimpleMappingExceptionResolver
*/
@Bean
public SimpleMappingExceptionResolver geSimpleMappingExceptionResolver() {
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
Properties mappings = new Properties();
/**
* 参数一:异常的类型,必须是异常类型的全名
* 参数二:视图名称
*/
mappings.put("java.lang.ArithmeticException", "error1");
mappings.put("java.lang.NullPointerException", "error2");
// 设置异常与视图的映射信息
resolver.setExceptionMappings(mappings);
return resolver;
}
}
5.自定义HandlerExceptionResolver类处理异常
/**
* 通过实现HandlerExceptionResolver做全局异常处理
*
* @author Lenovo
*
*/
@Configuration
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,Exception ex) {
ModelAndView mv = new ModelAndView();
// 判断不同异常类型,做不同视图跳转
if (ex instanceof ArithmeticException) {
mv.setViewName("error1");
}
if (ex instanceof NullPointerException) {
mv.setViewName("error2");
}
mv.addObject("error", ex.toString());
return mv;
}
}