zoukankan      html  css  js  c++  java
  • SpringBoot学习14:springboot异常处理方式4(使用SimpleMappingExceptionResolver处理异常)

    修改异常处理方法3中的全局异常处理Controller即可

    package bjsxt.exception;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
    
    import java.util.Properties;
    
    /**
     * Created by Administrator on 2019/2/14.
     * 全局异常处理类,使用SimpleMappingExceptionResolver 做全局异常处理
     * 优点:直接在一个方法里对需要处理的异常跳转不同的视图,比较简单方便
     * 缺点:无法把错误信息传递到视图层
     */
    
    @Configuration
    public class GlobalException {
    
        /**
         *
         * @return
         */
        @Bean
        public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
            SimpleMappingExceptionResolver resolver=new SimpleMappingExceptionResolver();
            Properties properties=new Properties();
            /**
             * 参数一:异常的类型,注意必须是异常类型的全名
             * 参数二:视图名称
             */
            properties.put("java.lang.ArithmeticException","error_arithmetic");
            properties.put("java.lang.NullPointerException","error_nullPointer");
            resolver.setExceptionMappings(properties);
            return resolver;
        }
    
    }
  • 相关阅读:
    这4点微不足道的改变,正在带我起飞
    “挑三拣四”地学一学Java I/O
    做到这3点,你也能成为一个高情商的人
    深入浅出一下Java的HashMap
    前端学习 -- Css -- 有序列表和无序列表
    Python 文件I/O
    python 模块
    python 函数
    Python 日期和时间
    python 字典
  • 原文地址:https://www.cnblogs.com/duanrantao/p/10374008.html
Copyright © 2011-2022 走看看