• SpringMVC异常处理


    SpringMVC异常简介
    系统中异常包括两类:预期异常(检查型异常)和运行时异常 RuntimeException,前者通过捕获异常从而获取异常信息, 后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。
    系统的 dao、service、controller 出现都通过 throws Exception 向上抛出,最后由 springmvc 前端控制器交由异常处理器进行异常处理,如下图

     

    异常处理具体实现


    1使用@ExceptionHandler注解处理异常


    缺点:只能处理当前Controller中的异常。

    1. @Controller
    2. public class ControllerDemo1 {
    3.     @RequestMapping("test1.action")
    4.     public String test1(){
    5.         int i = 1/0;
    6.         return "success.jsp";
    7.     }
    8.     @RequestMapping("test2.action")
    9.     public String test2(){
    10.         String s =null;
    11.         System.out.println(s.length());
    12.         return "success.jsp";
    13.     }
    14.     @ExceptionHandler(value ={ArithmeticException.class,NullPointerException.class} )
    15.     public ModelAndView handelException(){
    16.         ModelAndView mv =new ModelAndView();
    17.         mv.setViewName("error1.jsp");
    18.         return mv;
    19.     }
    20. }


    2使用:@ControllerAdvice+@ExceptionHandler

    全局使用,此处优先级低于局部异常处理器

    1. @ControllerAdvice
    2. public class GloableExceptionHandler1 {
    3.     @ExceptionHandler(value ={ArithmeticException.class,NullPointerException.class} )
    4.     public ModelAndView handelException(){
    5.         ModelAndView mv =new ModelAndView();
    6.         mv.setViewName("error1.jsp");
    7.         return mv;
    8.     }
    9. }


    配置包扫描

    <context:component-scan base-package="com.msb.service,com.msb.exceptionhandler"/>


    3使用:SimpleMappingExceptionResolver

     
    全局范围有效,在SpringMVC.xml配置

    1.     <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    2.         <property name="exceptionMappings">
    3.             <props>
    4.                 <prop key="java.lang.ArithmeticException">redirect:/error.jspprop>
    5.                 <prop key="java.lang.NullPointerException">redirect:/error2.jspprop>
    6.             props>
    7.         property>
    8.     bean>

    配置类配置

    1. /**
    2.  * 全局异常
    3.  */
    4.   @Configuration
    5.   public class GloableException2 {
    6.     @Bean
    7.     public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
    8.         SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
    9.         Properties prop = new Properties();
    10.         prop.put("java.lang.NullPointerException","error1.jsp");
    11.         prop.put("java.lang.ArithmeticException","error2.jsp");
    12.         resolver.setExceptionMappings(prop);
    13.         return resolver;
    14.     }
    15. }

    4自定义的HandlerExceptionResolver

    0xml配置,使用配置类代替配置文件,作用范围同方式三一样

    1. /**
    2.  * 全局异常
    3.  * HandlerExceptionResolve
    4.  */
    5.   @Configuration
    6.   public class GloableException3 implements HandlerExceptionResolver {
    7.     @Override
    8.     public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    9.         ModelAndView mv = new ModelAndView();
    10.         if(e instanceof NullPointerException){
    11.                 mv.setViewName("error1");
    12.         }
    13.         if(e instanceof ArithmeticException){
    14.                 mv.setViewName("error2");
    15.         }
    16.         mv.addObject("msg",e);
    17.         return mv;
    18.     }}

  • 相关阅读:
    BMC Helix解决方案落地亚马逊云科技中国区域,同时上线Marketplace
    Java多线程-线程生命周期(一)
    Are Large Language Models Really Robust to Word-Level Perturbations?
    nodejs 和 npm 版本对应关系
    【Swift 60秒】61 - Computed properties
    【七夕节】html+css+JavaScript+服务器 给女朋友的七夕过节网站
    【制作100个unity游戏之29】使用unity复刻经典游戏《愤怒的小鸟》(完结,附带项目源码)
    k8s client-go源码分析 informer源码分析(3)-Reflector源码分析
    [CISCN2019 华北赛区 Day1 Web2]ikun
    推荐一个基于.Net Framework开发的Windows右键菜单管理工具
  • 原文地址:https://blog.csdn.net/listeningdu/article/details/128144556