zoukankan      html  css  js  c++  java
  • SpringBoot学习15:springboot异常处理方式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;
        }
    }
  • 相关阅读:
    git更新或者还原本地代码
    log4net配置文件
    用ASP.NET MVC仿站糗事百科
    为表创建索引
    VisualStudio2008+水晶报表的使用
    C#中的位的或运算的理解
    char.IsLetter的使用
    C# 邮箱的使用
    NPOI DataTable导出excel
    NPOI DataSet导出excel
  • 原文地址:https://www.cnblogs.com/duanrantao/p/10374192.html
Copyright © 2011-2022 走看看