zoukankan      html  css  js  c++  java
  • SpringBoot: 15.异常处理方式5(通过实现HandlerExceptionResolver类)(转)

     

    修改异常处理方式4中的全局异常处理controller

    复制代码
    package com.bjsxt.exception;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.lang.Nullable;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Properties;
    
    /**
     * Created by Administrator on 2019/2/14.
     * 全局异常处理类,通过实现 HandlerExceptionResolver类做全局异常处理
     * 优点:相对于方法4可以传递异常信息
     */
    
    @Configuration
    public class GlobalException implements HandlerExceptionResolver{
    
    
        @Override
        public ModelAndView resolveException(HttpServletRequest httpServletRequest,
               HttpServletResponse httpServletResponse, @Nullable Object o, Exception e) {
    
            ModelAndView mv = new ModelAndView();
            //判断不同异常类型,做不同视图跳转
            if(e instanceof ArithmeticException){
                mv.setViewName("error_arithmetic");
            }
            if(e instanceof NullPointerException){
                mv.setViewName("error_nullPointer");
            }
            mv.addObject("msg", e.toString());
            return mv;
        }
    }
    复制代码
  • 相关阅读:
    mysql权限
    Win7_64位使用Mysql Odbc
    二叉树的遍历
    Notepad++的使用
    mysql与mysqld
    Mysql 聚集函数和分组
    Linux 目录
    Linux 倒引号、单引号、双引号
    openkm安装过程
    rhel 7 设置默认运行级别为图形
  • 原文地址:https://www.cnblogs.com/kuangzhisen/p/10427186.html
Copyright © 2011-2022 走看看