SpringBoot--SpringBoot 5种异常处理机制

29 篇文章 0 订阅
1 篇文章 0 订阅

SpringBoot 中异常处理

SpringBoot 中对于异常处理提供了五种处理方式

五种处理方式顺序:

  1. 局部的异常处理
  2. 全局的异常处理
  3. 实现 HandlerExceptionResolver 接口的异常处理
  4. 返回 SimpleMappingExceptionResolver 对象的异常处理
  5. 自定义 error 页面

   1 自定义错误页面

   SpringBoot 默认的处理异常的机制:
      SpringBoot默认的已经提供了一套处理异常的机制。 一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 SpringBoot 中提供了一个名为 BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息。
      如果我们需要将所有的异常统一跳转到自定义的错误页面,需要再src/main/resources/templates 目录下创建 error.html 页面。注意:页面名称必须叫 error

    1.1 处理代码
    @RequestMapping("/index")
    public String index(){
        String str=null;
        int length = str.length();
        return "ok";
    }
     1.2 处理视图

在这里插入图片描述

     1.3 处理结果

在这里插入图片描述


   2 局部处理:使用@ExceptionHandler 注解

    2.1 处理代码
    @RequestMapping("/index")
    public String index(){
        String str=null;
        int length = str.length();
        return "ok";
    }

    @RequestMapping("/byZero")
    public String byZero(){
        int i=10/0;
        return "ok";
    }

    @ExceptionHandler(value = {java.lang.NullPointerException.class,java.lang.ArithmeticException.class})
    public ModelAndView handlerNullException(Exception e){
        ModelAndView view = new ModelAndView();
        view.setViewName("smallError");
        view.addObject("smallError",e.toString());
        return view;

    }
    2.2 处理视图

在这里插入图片描述

    2.3 处理结果

在这里插入图片描述在这里插入图片描述

  3 全局处理:使用@ExceptionHandler 加@ControllerAdvice 注解

    3.1 处理代码
/**
 * @author 守鹤
 * @date 2020/5/14 0:06
 */
@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(value = {java.lang.NullPointerException.class})
    public ModelAndView globalNullPoint(Exception e){
        ModelAndView view = new ModelAndView();
        view.setViewName("globalError");
        view.addObject("globalError",e.toString());
        return view;
    }
}
    3.2 处理视图

在这里插入图片描述

    3.3 处理结果

在这里插入图片描述


  4 自定义处理:使用SimpleMappingExceptionResolver 对象

    4.1 处理代码
@Configuration
public class GlobalSimpleMappingException {
    @Bean
    //此方法返回值必须是 SimpleMappingExceptionResolver 对象
    public SimpleMappingExceptionResolver getSimpleException(){
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        Properties properties = new Properties();
        /**
		* 参数一:异常类型,并且是全名 
		* 参数二:视图名称 
		* */
* 		properties.setProperty("java.lang.NullPointerException","simpleNullError");
        properties.setProperty("java.lang.ArithmeticException","simpleArithmeError");
        resolver.setExceptionMappings(properties);
        return resolver;
    }
}
    4.2 处理视图

在这里插入图片描述在这里插入图片描述

    4.3 处理结果

在这里插入图片描述在这里插入图片描述


  5 自定义处理:实现HandlerExceptionResolver接口

    5.1 处理代码
/**
 * @author 守鹤
 * @date 2020/5/14 20:46
 */
@Configuration
public class MyExceptionHandler implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

        ModelAndView modelAndView = new ModelAndView();
        if(e instanceof  NullPointerException){
            modelAndView.setViewName("MyNullError");
        }
        if(e instanceof  ArithmeticException){
            modelAndView.setViewName("MyArithmeError");
        }
        modelAndView.addObject("myError",e.toString());
        return modelAndView;
    }
}
    5.2 处理视图

在这里插入图片描述在这里插入图片描述

    5.3 处理结果

在这里插入图片描述在这里插入图片描述

SpringBoot 5种异常处理机制 demo

链接:https://pan.baidu.com/s/1J3a5jgeVLNFxlu3JPjehtA
提取码:3moc

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值