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;
        }
    }
  • 相关阅读:
    数据库三范式(转)
    Tyrion中文文档(含示例源码)
    mongodb数据库导入导出恢复
    HTTP协议:Content-Type
    requests爬虫组件
    JS 数组对象
    JS 函数
    javascript window对象属性和方法
    js String对象
    Math对象-JavaScript
  • 原文地址:https://www.cnblogs.com/duanrantao/p/10374192.html
Copyright © 2011-2022 走看看