13.1、异常处理——HandlerExceptionResolver 和@ExceptionHandler

Spring MVC通过HandlerExceptionResolver处理程序的异常,包括Handler的映射、数据绑定以及目标方法的执行。HandlerExceptionResolver时一个接口,该接口的实现类都有处理异常的功能。HandlerExceptionResolver是该接口应用广泛的一个实现类,并且DispatcherServlet默认装配了HandlerExceptionResolver 的Bean。使用HandlerExceptionResolver处理异常时,要在spring配置文件中配置。
示例如下:
1、web.xml和spring的配置文件
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

spring的配置文件springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:component-scan base-package="com.lzj.springmvc"></context:component-scan>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

2、创建请求
在WebContent下面创建index.jsp,内容为:

<a href="springMVC/testExceptionHandlerExceptionResolver?i=0">Test ExceptionHandlerExceptionResolver</a>

3、创建请求控制器
控制器用于截获index.jsp中的请求

@Controller
@RequestMapping("/springMVC")
public class TestSpringMVC {

    @RequestMapping("/testExceptionHandlerExceptionResolver")
    /*请求中的i的值为0,当控制器获取该值,执行10/0时出现异常,导致程序失败*/
    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){
        System.out.println("result: " + (10/i));
        return "success";
    }
}

当执行index.jsp中请求后,控制器截获请求,并获取请求中的请求参数i,由于i的值为0,控制器中的逻辑为10/0,运行时会抛出一个异常ArithmeticException

4、处理异常
在TestSpringMVC控制器类中添加处理异常的方法

public class TestSpringMVC {

    //ExceptionHandler注解表示处理异常的类型
    @ExceptionHandler({ArithmeticException.class})
    public String handleArithmeticException(Exception exception){
        System.out.println("异常: " + exception);
        return "success";
    }

    @RequestMapping("/testExceptionHandlerExceptionResolver")
    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){
        System.out.println("testExceptionHandlerExceptionResolver");
        System.out.println("result: " + (10/i));
        return "success";
    }

当index.jsp中发送请求后, testExceptionHandlerExceptionResolver控制器截获请求,并获取请求中i值后,在执行到System.out.println(“result: ” + (10/i));,出现异常,程序不在往下执行,然后handleArithmeticException处理器捕获ArithmeticException异常,处理异常程序。
Console中输出内容如下:

testExceptionHandlerExceptionResolver
异常: java.lang.ArithmeticException: / by zero

5、处理异常优先级
控制器中处理异常的方法改为如下

@Controller
@RequestMapping("/springMVC")
public class TestSpringMVC {

    @ExceptionHandler({RuntimeException.class})
    public String handleArithmeticException2(Exception exception){
        System.out.println("handleArithmeticException2");
        System.out.println("异常: " + exception);
        return "success";
    }

    @RequestMapping("/testExceptionHandlerExceptionResolver")
    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){
        System.out.println("testExceptionHandlerExceptionResolver");
        System.out.println("result: " + (10/i));
        return "success";
    }
}

当发送index.jsp中请求后,testExceptionHandlerExceptionResolver控制器方法截获该请求,并获取请求参数i后,执行逻辑出现异常,抛出ArithmeticException异常,异常被RuntimeException类型的异常捕获,handleArithmeticException2异常处理该异常。因为ArithmeticException异常继承自RuntimeException,所以RuntimeException类型的异常可以捕获ArithmeticException异常。Console中输出内容如下:

testExceptionHandlerExceptionResolver
handleArithmeticException2
异常: java.lang.ArithmeticException: / by zero

但是当程序中同时出现了RuntimeException和ArithmeticException异常处理程序,执行顺序是什么呢?
处理异常发方法如下:

@Controller
@RequestMapping("/springMVC")
public class TestSpringMVC {

    @ExceptionHandler({RuntimeException.class})
    public String handleArithmeticException2(Exception exception){
        System.out.println("handleArithmeticException2");
        System.out.println("异常: " + exception);
        return "success";
    }

    @ExceptionHandler({ArithmeticException.class})
    public String handleArithmeticException(Exception exception){
        System.out.println("异常: " + exception);
        return "success";
    }

    @RequestMapping("/testExceptionHandlerExceptionResolver")
    public String testExceptionHandlerExceptionResolver(@RequestParam("i") int i){
        System.out.println("testExceptionHandlerExceptionResolver");
        System.out.println("result: " + (10/i));
        return "success";
    }
}

发送index.jsp中的请求后,testExceptionHandlerExceptionResolver控制器方法截获请求,并获取请求中的参数i值0,控制器方法出现异常,异常先被handleArithmeticException方法捕获执行,所以异常会先被标记最底层实现类的那个异常程序处理,如果没有的话,一级一级的往上抛。
Console中输出内容如下:

testExceptionHandlerExceptionResolver
异常: java.lang.ArithmeticException: / by zero

注意:如果把异常处理程序和控制器处理方法写在同一个类中,那么异常处理器只能处理该类中抛出的异常。如果想让异常处理程序能处理项目下所有类抛出的异常,可以在src下新建一个包,专门放置处理异常的异常。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@ControllerAdvice 是一个注解,用于定义全局异常处理器类。我们可以在该类中定义多个 @ExceptionHandler 注解方法来处理不同类型的异常。当系统中出现异常时,会根据异常类型找到对应的 @ExceptionHandler 方法进行处理。 具体的步骤如下: 1. 创建一个类,并用 @ControllerAdvice 注解标记。 2. 在该类中定义多个方法,每个方法上使用 @ExceptionHandler 注解,并传入对应的异常类型作为参数。 3. 在方法中编写具体的异常处理逻辑。 例如,我们可以创建一个全局异常处理器类 GlobalExceptionHandler: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e) { // 处理 Exception 类型的异常 // 返回自定义的错误信息 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error"); } @ExceptionHandler(RuntimeException.class) public ResponseEntity<String> handleRuntimeException(RuntimeException e) { // 处理 RuntimeException 类型的异常 // 返回自定义的错误信息 return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Bad Request"); } // 可以添加更多的异常处理方法... } ``` 在上述例子中,我们定义了两个异常处理方法,分别处理 Exception 类型和 RuntimeException 类型的异常。当系统中抛出对应的异常时,会调用相应的处理方法并返回自定义的错误信息。 这样,在整个系统中任何地方出现的对应类型的异常都会被统一处理,提高了代码的可维护性和错误处理的一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值