• springboot异常处理


    package com.example.exception;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.NoHandlerFoundException;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Configuration
    public class GlobalException implements HandlerExceptionResolver {
        /**
         * 全局异常处理 (自动指向页面,并自动分配异常数据到模板中)
         *
         * @param httpServletRequest
         * @param httpServletResponse
         * @param o
         * @param e
         * @return
         */
        @Override
        public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
            ModelAndView modelAndView = new ModelAndView();
    
    
            if (e instanceof ArithmeticException) {// 数字异常
                modelAndView.setViewName("error/error1");
            }
    
    
            if (e instanceof NullPointerException) {// 空指钊异常
                modelAndView.setViewName("error/error2");
            }
    
    
            if (e instanceof NoHandlerFoundException) {//404
                modelAndView.setViewName("error/404");
            }
    
            modelAndView.addObject("error", e.toString());
    
            return modelAndView;
        }
    }
    

      

  • 相关阅读:
    Running OOM killer script for process 32248 for Solr on port 8983
    List删除元素
    Oracle联合主键
    synchronized的四种用法
    数据库 乐观锁与悲观锁
    noip2011普及组 统计单词数
    bzoj3751 noip2014解方程
    汕头市队赛SRM07
    noip2010 导弹拦截&&vijos1810
    noip2009普及组 细胞分裂&&vijos1814
  • 原文地址:https://www.cnblogs.com/leigepython/p/10132462.html
Copyright © 2020-2023  润新知