• 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.     }}

  • 相关阅读:
    如何查看go依赖包的license (glice)
    【2014年数据结构真题】
    NL50-MPI 西门子MPI转以太网通讯模块
    Java abstract 关键字(keyword)
    数组的左旋和右旋算法
    某安全设备frp流量告警分析
    IntelliJ IDEA 2022.2 正式发布:已完全支持 Spring 6 和 Spring Boot 3了吗?
    随机手机号查询易语言代码
    高防服务器是怎样进行防御的?
    进行股票量化交易接口程序化开发要注意的事项
  • 原文地址:https://blog.csdn.net/listeningdu/article/details/128144556