• 14SpringMVC中的默认的异常解析器,以及如何基于XML和注解的方式配置自定义异常解析器


    异常处理器

    默认异常解析器

    控制器方法执行过程中如果出现了异常,此时就可以使用SpringMVC提供的HandlerExceptionResolver接口帮助我们处理出现的异常

    • HandlerExceptionResolver接口有DefaultHandlerExceptionResolver(默认)和SimpleMappingExceptionResolver(自定义)两种实现类

    DefaultHandlerExceptionResolver的doResolveException方法处理完控制器方法中出现的异常后会返回一个新的ModelAndView对象用来跳转到指定的异常页面

    @Nullable
        protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {
            try {
                // 处理请求方式不被支持的异常
                if (ex instanceof HttpRequestMethodNotSupportedException) {
                    return this.handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException)ex, request, response, handler);
                }
    
                if (ex instanceof HttpMediaTypeNotSupportedException) {
                    return this.handleHttpMediaTypeNotSupported((HttpMediaTypeNotSupportedException)ex, request, response, handler);
                }
    
                if (ex instanceof HttpMediaTypeNotAcceptableException) {
                    return this.handleHttpMediaTypeNotAcceptable((HttpMediaTypeNotAcceptableException)ex, request, response, handler);
                }
    
                if (ex instanceof MissingPathVariableException) {
                    return this.handleMissingPathVariable((MissingPathVariableException)ex, request, response, handler);
                }
                //........
            } catch (Exception var6) {
                if (this.logger.isWarnEnabled()) {
                    this.logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", var6);
                }
            }
    
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    自定义异常解析器

    配置SpringMVC提供的自定义异常处理器SimpleMappingExceptionResolver需要设置两个属性

    设置Properties集合类型的exceptionMappings属性

    • properties的键是异常的全类名表示处理器方法执行过程中出现哪种异常时才会被异常解析器处理
    • properties的值是视图名称: 表示若出现指定异常时要跳转到的页面,此时依然遵循视图解析器的规则

    设置String类型的exceptionAttribute属性用来把异常对象共享到请求域中,所以我们需要设置一个key将来才能在页面中通过这个key获取到请求域中的异常对象的信息

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                
                <prop key="java.lang.ArithmeticException">errorprop>
            props>
        property>
        
        <property name="exceptionAttribute" value="ex">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    编写控制器方法模拟数学运算异常,指定跳转到的异常页面并展示异常信息

    @RequestMapping("/testExceptionHandler")
    public String testExceptionHandler(){
        System.out.println(1/0);
        return "success";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    出现错误
    
    <p th:text="${ex}">p>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    基于注解的异常处理

    @ControllerAdvice注解是@Controller的扩展注解,也可以将当前类标识为异常处理的组件,一般会放在controller包下

    @ExceptionHandler注解的value属性可以用来指定一个或多个异常,当控制器方法执行过程中出现了指定类型的异常就会执行其所标识的控制器方法

    • Exception(Throwable) ex: 对于控制器方法执行中出现的异常信息对象,我们只要在控制器方法中声明对应形参前端控制器会自动注入Exception类型的对象
    @ControllerAdvice
    public class ExceptionController {
        // 设置所标识方法处理的异常
        @ExceptionHandler(ArithmeticException.class)
        public String handleArithmeticException(Throwable ex, Model model){
            // 将异常对象放在请求域中共享
            model.addAttribute("ex", ex);
            // 设置出现异常后跳转的页面
            return "error";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    systemverilog学习 ---- 进程
    Vagrant 搭建虚拟机环境
    常见的测试理论面试问题
    xxis not in the sudoers file. This incident will be reported.
    react之Component存在的2个问题
    机器学习——特征工程
    LeetCode693. 交替位二进制数
    【RPC】gRPC 安装及使用
    技能学习链接
    Non-zero exit code pycharm
  • 原文地址:https://blog.csdn.net/qq_57005976/article/details/133792042