一)SpringBoot提供的异常处理
1.自定义异常页面
SpringBoot内置了一套错误页面的显示
@RequestMapping(value = "/show1")
public String showInfo1(){
String str = null;
str.length();
return "index";
}
SpringBoot 默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。 一旦程序中出现了异常 SpringBoot 会像/error 的 url 发送请求。在 springBoot 中提供了一个 叫 BasicExceptionController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常 信息。
查看BasicExceptionController ,在类上定义了默认的路径为/error
@Controller
@RequestMapping({"${server.error.path:${error.path:/error}}"})
public class BasicErrorController extends AbstractErrorController {
private final ErrorProperties errorProperties;
public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
this(errorAttributes, errorProperties, Collections.emptyList());
}
在templates文件夹内创建一个页面error,必须叫这个的名字,当程序出现错误的时候,所有的错误在这个页面显示,自定义的异常页面默认是拦截所有的异常,所有的异常信息都会来到这个的页面。
二)注解@ExceptionHandler注解的使用
使用注解实现指定异常跳转到指定的页面
/**
* @param
* @description: 此方法用于拦截空指针异常,当出现了控制异常会跳转到指定的页面
* @return:
* @author: shinelon
* @time: 2019/8/24:16:59
*/
@ExceptionHandler(value = {NullPointerException.class})
public ModelAndView exception(Exception e) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", e.getMessage());
modelAndView.setViewName("error2");
return modelAndView;
}
三)使用@ExceptionHandler注解和@ControlleActive注解能够实现全局异常处理
@ControllerAdvice
public class GlobalHandlerExceptionResolver {
/**
* 所有的控制器发生了异常都会找到此方法
* @param e 异常
* @return 返回视图 进行错误页面的跳转并将错误信息传递到页面上
*/
@ExceptionHandler(Exception.class)
public ModelAndView handle(Exception e) {
ModelAndView mv =new ModelAndView();
mv.setViewName("error/exception");
mv.addObject("message", e.getMessage());
return mv;
}
/**
* 对Model对象进行键值对的赋值
* @param model
*/
@ModelAttribute
public void addModel(Model model) {
model.addAttribute("aa", "你好前端全局控制器");
}
}
四)配置 SimpleMappingExceptionResolver 处理异常
1.4.1 在全局异常类中添加一个方法完成异常的同一处理
/**
* 通过 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.NullPointerException", "error2");
//设置异常与视图映射信息的
resolver.setExceptionMappings(mappings);
return resolver;
}
}
五)实现HandlerExceptionResolver 接口实现异常处理。
/**
* 通过实现 HandlerExceptionResolver 接口做全局异常处理
*
*
*/
@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;
}
}